PHP curl超時問題


    今天調試一個非常老的代碼時  發現nginx服務器超時  改了下nginx配置

    發現是后台腳本一直等待  排查到最后發現是curl 超時引起的等待

    具體解決方案:

curl_setopt( $this->ch, CURLOPT_URL, $url );
curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $this->ch, CURLOPT_TIMEOUT_MS,3000);      //  3秒超時
curl_setopt( $this->ch, CURLOPT_HEADER, 0 );
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, false );

 

    PHP curl的超時主要有4個參數 原文如下;

          There's a very distinctive difference between these two configurations within cURL. I'll try to define them for you, and then provide you a very common example which I share to people who I teach about cURL.

CURLOPT_CONNECTTIMEOUT is designed to tell the script how long to wait to make a successful connection to the server before starting to buffer the output. A destination's server which may be overloaded, offline or crashed would probably make this setting become useful.

CURLOPT_TIMEOUT is designed to tell the script how long to wait to receive a completely buffered output from the server. A destination's huge file, slow connection speeds or slow rendering would probably make this setting become useful.

A good example of where these will both apply to, is when you're telling cURL to download a MP3 file. CURLOPT_CONNECTTIMEOUT would be set at about 10 seconds which would mean that if no response is provided within 10 seconds then the script will abort, and CURLOPT_TIMEOUT would be set at about 100 seconds which would mean if the MP3 has not downloaded within 100 seconds then abort the script. It's the best way of explaining it to developers.

具體是:

   CURLOPT_CONNECTTIMEOUT 

     建立連接時候的超時設置

  CURLOPT_TIMEOUT 

     接收信息時的超時設置

   CURLOPT_CONNECTTIMEOUT_MS,   CURLOPT_TIMEOUT_MS 意思相同 只是超時單位為毫秒了。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM