php - 使用 curl 获取 url 响应的值

标签 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;

为什么我总是收到 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/

相关文章:

php - 提交表单后如何更改级别访问权限?

java - Android 中的 HTTP-PUT 请求

java - 如何在 Java 中模拟 PHP cURL 请求?

javascript - javascript 中的curl -u 请求

PHP cURL 与部分传输

amazon-web-services - 我正在尝试 curl GET 请求

php - 深入解释 Magento 的 Store/Store View/Website 层?

php图片上传安全防范黑客攻击

一段时间后PHP重置 session

php - 切换到 mysqli 是个好主意吗?