2011-08-01 38 views
0

我正在使用curl從URL檢索數據。curl返回「從對等方接收數據時失敗」

如果PHP代碼是通過HTTP請求調用的,或者URL是在Firefox中輸入的,那麼一切正常。如果從PHP CLI腳本執行完全相同的代碼,curl_exec將返回false。錯誤信息是「從對方接收數據時失敗」。

任何想法爲什麼捲曲不起作用?

當我設置捲曲輸出到長篇大論,我得到:

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); 
+0

您提供的代碼不完整。請將其降至所需的最小值以顯示您的案例:刪除'$ this'依賴項,而不是'watchdog'函數調用,只需'die()'。否則,這裏的人不能重現你的問題。有問題的URL也丟失。 – hakre

+0

對不起,我不能發佈URL,因爲我必須保密(這不是我的決定!)。 – BetaRide

+0

畢竟,我認爲從cli和通過Web服務器調用curl是有區別的嗎? – BetaRide

回答

0

試試wget的。如果這也失敗了,但是您可以從另一個IP /設備訪問該地址,這可能意味着您的IP被阻止或被防火牆/ nginx反ddos攻擊過濾掉了。嘗試代理。