【发布时间】:2015-08-20 09:14:54
【问题描述】:
我使用 cURL 从另一个网站获取数据。有时显示数据,有时显示空结果
这是我的代码
函数 get_data($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $agent=$_SERVER["HTTP_USER_AGENT"]; curl_setopt($ch,CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); curl_close($ch); 返回$数据; } $returned_content = get_data('www.example.com'); 回声 $returned_content;【问题讨论】:
-
尝试使用
curl_error($ch);php.net/curl_error检查失败时会发生什么 -
对我来说工作得很好,也许你连接有问题。
-
可能与您请求的页面有关,但您可以尝试此功能,因为它有一些错误处理:geneticcoder.blogspot.com/2015/02/curl-function-for-php.html
-
尝试在
echo $returned_content之后添加exit;; -
顺便说一句
www.example.com不是 URL,您缺少协议。