Get url response value with curl

I am using the PHP curl method to get a string type response. To create a query, 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 do I always get the answer bool (true) instead of the string I echo on the other hand?

thank

+3
source share
1 answer

Since you already have

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

in your code. curl_exec should already return the contents of the page instead of BOOL.

This is a fragment of the library that I am using. As indicated, this might not be necessary, but it helped me once ...

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

There are also some errors related to CURLOPT_NOBODY (which may explain why you have this problem):

+10

Source: https://habr.com/ru/post/1727273/


All Articles