tags:

views:

142

answers:

1

Hi,

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

+2  A: 

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):

AlexV
He wants the body to be returned, with `CURLOPT_NOBODY` the body won't even be downloaded.
Wim
This is why it is set to FALSE. If set to TRUE it will not download any content read more carefully before voting down!
AlexV
Ok you're right. Still, the default for `CURLOPT_NOBODY` is false so there's no need to set this. Also, you're supposed to explain your answer -- I even had to scroll your code to the right before I could read that comment.
Wim
Edited answer for readability and added some info.
AlexV
Great! Removed my downvote. Can't vote up apparently, "vote can't be changed unless answer is edited". Duh...
Wim
No problem. I just hope that my asnwer / our comments will help solve question.
AlexV