curl返回“从对端接收数据失败”

huwehgph  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(318)

我正在使用curl从一个URL检索数据。
如果通过HTTP请求调用php代码或在Firefox中输入URL,一切都正常。如果从PHP CLI脚本执行完全相同的代码,curl_exec将返回false。错误消息是“从对等方接收数据时失败”。
你知道为什么curl不工作吗?
当我将curl输出设置为verbose时,我得到:

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);
qmb5sa22

qmb5sa221#

尝试wget。如果同样失败,但您可以从另一个IP/设备访问该地址,这可能意味着您的IP被防火墙/nginx Antiddos攻击阻止或过滤掉。尝试代理。

相关问题