PHP cURL GET request and request's body

前端 未结 5 1278
礼貌的吻别
礼貌的吻别 2020-12-05 00:23

i\'m trying using cURL for a GET request like this:

function connect($id_user){
    $ch = curl_init();
    $headers = array(
    \'Accept: application/json\'         


        
5条回答
  •  渐次进展
    2020-12-05 00:29

    CURLOPT_POSTFIELDS as the name suggests, is for the body (payload) of a POST request. For GET requests, the payload is part of the URL in the form of a query string.

    In your case, you need to construct the URL with the arguments you need to send (if any), and remove the other options to cURL.

    curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    //$body = '{}';
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
    //curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

提交回复
热议问题