我正在使用 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;
为什么我总是收到 bool(true) 响应而不是我从另一端回显的字符串?
谢谢
最佳答案
既然你已经有了
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
在您的代码中。 curl_exec 应该已经返回页面的内容而不是 BOOL。
这是我使用的库的一个片段。正如所指出的那样,这可能不需要,但它帮助了我一次......
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
它似乎还有一些与 CURLOPT_NOBODY 相关的错误(这可能解释了您遇到此问题的原因):
关于php - 使用 curl 获取 url 响应的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2001897/