2017-08-30 108 views
0

我想使用Cloudflare服務背後的curl獲取網站的內容。以下代碼適用於大部分網站,但不包括Cloudflare後面的代碼,甚至在cloudflare.com上也失敗。你能幫我解決可能導致這種行爲的原因嗎?Curl操作超時,收到0個字節中的0個

我有以下配置:

  • PHP版本31年6月5日
  • 捲曲:7.54.0

代碼:

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds 
curl_setopt($ch, CURLOPT_URL, "https://cloudflare.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
//curl_setopt($ch, CURLOPT_SSLVERSION, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($ch, CURLOPT_VERBOSE, true); 

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w'); 
curl_setopt($ch, CURLOPT_STDERR, $fp); 

// $output contains the output string 
$result = curl_exec($ch); 

if ($result === FALSE) { 
    printf("cUrl error (#%d): %s<br>\n", curl_errno($ch), 
      htmlspecialchars(curl_error($ch))); 
} 

fclose($fp); 
curl_close($ch); 

結果是:

cUrl error (#28): Operation timed out after 5000 milliseconds with 0 out of 0 bytes received 

錯誤日誌:

* Rebuilt URL to: https://cloudflare.com/ 
* Trying 198.41.214.162... 
* TCP_NODELAY set 
* Connected to cloudflare.com (198.41.214.162) port 443 (#0) 
* ALPN, offering http/1.1 
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH 
* successfully set certificate verify locations: 
* CAfile: /etc/pki/tls/certs/ca-bundle.crt 
    CApath: none 
* Operation timed out after 5000 milliseconds with 0 out of 0 bytes received 
* stopped the pause stream! 
* Closing connection 0 
+0

您的代碼適用於我。也許這是你的網絡問題。 –

+0

它也適用於我在不同的服務器上。這就是爲什麼我不確定下一步該去哪裏... –

+0

任何時候你依賴外部資源,它永遠不會失敗,它會下降,不可用等等,你只需要計劃它,並且提出一個適應外部資源停機時間的解決方案。 –

回答

0

您的代碼工作正常。我認爲這是對你網絡問題。

相關問題