使用curl获取url响应的值

Luc*_*cia 3 php curl

我正在使用PHP curl方法来获取字符串类型响应.要创建我使用的请求:

$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;
Run Code Online (Sandbox Code Playgroud)

为什么我总是收到一个bool(true)响应,而不是我从另一方回应的字符串?

谢谢

Ale*_*exV 10

既然你已经拥有了

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Run Code Online (Sandbox Code Playgroud)

在你的代码中.curl_exec应该已经返回页面的内容而不是BOOL.

这是我使用的库的片段.正如所指出的那样,这可能不需要,但它曾帮我一次......

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

它似乎也有一些与CURLOPT_NOBODY相关的错误(这可能解释了为什么你有这个问题):