3

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

Lucia
  • 4,657
  • 6
  • 43
  • 57
  • What do you mean by '"true" response'? Is $response the string "true", or is it boolean true? – GZipp Jan 04 '10 at 20:41

1 Answers1

10

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
  • 22,658
  • 18
  • 85
  • 122
  • He wants the body to be returned, with `CURLOPT_NOBODY` the body won't even be downloaded. – Wim Jan 04 '10 at 20:36
  • 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 Jan 04 '10 at 20:45
  • 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 Jan 04 '10 at 20:51
  • Great! Removed my downvote. Can't vote up apparently, "vote can't be changed unless answer is edited". Duh... – Wim Jan 04 '10 at 21:07
  • No problem. I just hope that my asnwer / our comments will help solve question. – AlexV Jan 04 '10 at 21:18