使用 curl 获取 url 响应的值 [英] get the value of an url response with curl
问题描述
我正在使用 PHP curl 方法来获取字符串类型的响应.创建我使用的请求:
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;
为什么我总是收到 bool(true) 响应而不是我从另一端回显的字符串?
Why I always receive a bool(true) response instead of the string I echo from the other side?
谢谢
推荐答案
既然你已经有了
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
在您的代码中.curl_exec 应该已经返回页面的内容而不是 BOOL.
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);
此外,它似乎还有一些与 CURLOPT_NOBODY 相关的错误(这可能解释了您遇到此问题的原因):
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
- http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
- http://curl.haxx.se/mail/curlphp-2008-03/0072.html
这篇关于使用 curl 获取 url 响应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!