1

I am making a curl call using PHP to a PHP based URL and getting into some problems.

Please note that the when I add the Content-Type in the header, it causes the endpoint not to read my params at all.

Any help is appriciated.

Here is what I am doing and the result I am getting:

With Header but no Content-Type in Header:

$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
    "Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);

// Status 200 // $result becomes = "{"flag":0,"message":"Token not found in request","data":[]}"

With Header with Content-Type in Header:

*$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
        'Content-Type: application/json',
        "Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);*

// Status 200 // $result becomes = "{"flag":0,"message":"No Request.","data":[]}"


RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Do you control the API code? – RiggsFolly Jan 27 '23 at 16:41
  • Unfortunately no. – Adeel Imtiaz Jan 27 '23 at 16:44
  • 1
    Why do you want to set the content type as JSON when the content isn't JSON? If the server tries to parse the content based on that header, it won't be able to and see it as if you sent invalid data. – M. Eriksson Jan 27 '23 at 16:46
  • I did some research and came across https://stackoverflow.com/questions/44882777/php-curl-using-post-raw-json-data and followed the answer there and was unable to get through the No request message. I updated my post params to: `$curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));`. I also updated the params to be an array. – Adeel Imtiaz Jan 27 '23 at 17:07

0 Answers0