get the value of an url response with curl

I am using PHP curl method to get a string type response. To create the request I use:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, $data);

$response = curl_exec($ch);
$err = curl_error($ch);

curl_close($ch);

if($response === false)
    throw new Exception(__CLASS__."::".__FUNCTION__."_".$err);
return $response;

Why I always receive a bool(true) response instead of the string I echo from the other side?

Thanks

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Since you already have

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

in your code. curl_exec should already returns the content of the page instead of a BOOL.

This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once…

//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);

Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply