【发布时间】:2011-08-01 12:41:51
【问题描述】:
我正在使用 curl 从 URL 检索数据。
如果通过 HTTP 请求调用 php 代码或在 Firefox 中输入 URL,则一切正常。如果从 PHP CLI 脚本执行相同的代码,curl_exec 将返回 false。错误信息是“从对端接收数据失败”。
任何想法为什么 curl 不起作用?
当我将 curl 输出设置为详细时,我得到:
Setting curl to verbose gives:
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Last-Modified: Mon, 01 Aug 2011 13:04:59 GMT
< Cache-Control: no-store
< Cache-Control: no-cache
< Cache-Control: must-revalidate
< Cache-Control: pre-check=0
< Cache-Control: post-check=0
< Cache-Control: max-age=0
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Type: text/xml
< Transfer-Encoding: chunked
< Date: Mon, 01 Aug 2011 13:04:58 GMT
<
* Trying 153.46.254.70... * Closing connection #0
* Failure when receiving data from the peer
这是代码:
// if curl is not installed we trigger an alert, and exit the function
if (!function_exists('curl_init')){
watchdog('sixtk_api', 'curl is not installed, api call cannot be executed',array(),WATCHDOG_ALERT);
return $this;
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $this->request);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
// Download the given URL, and return output
$output = curl_exec($ch);
if (!$output) {
$error = curl_error($ch);
echo($error);
}
// Close the cURL resource, and free system resources
curl_close($ch);
【问题讨论】:
-
您收到的代码不完整。请将其减少到向您展示案例所需的最小值:删除
$this依赖项,而不是watchdog函数调用,只需die()。否则这里的人无法重现您的问题。相关网址也丢失了。 -
抱歉,我不能发布网址,因为我必须保密(这不是我的决定!)。
-
毕竟,我认为从 cli 调用 curl 和通过 Web 服务器调用 curl 是有区别的?
-
两者都可以有自己的配置。例如,用户代理可能会有所不同,但这是推测。 CLI 可能会被防火墙或 SELinux 等安全相关应用程序阻止。所以它可能取决于很多事情。您实际上可以做的是嗅探网络流量并查看哪个点阻止发送或接收数据。
标签: php curl command-line-interface