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":[]}"


4
  • Do you control the API code? Commented Jan 27, 2023 at 16:41
  • Unfortunately no. Commented Jan 27, 2023 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. Commented Jan 27, 2023 at 16:46
  • I did some research and came across stackoverflow.com/questions/44882777/… 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. Commented Jan 27, 2023 at 17:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.