Setting Curl's Timeout in PHP

前端 未结 7 2099
你的背包
你的背包 2020-11-22 14:20

I\'m running a curl request on an eXist database through php. The dataset is very large, and as a result, the database consistently takes a long amount of time to return an

7条回答
  •  一生所求
    2020-11-22 14:25

    You will need to make sure about timeouts between you and the file. In this case PHP and Curl.

    To tell Curl to never timeout when a transfer is still active, you need to set CURLOPT_TIMEOUT to 0, instead of 1000.

    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    

    In PHP, again, you must remove time limits or PHP it self (after 30 seconds by default) will kill the script along Curl's request. This alone should fix your issue.
    In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort:

    # The maximum execution time, in seconds. If set to zero, no time limit is imposed.
    set_time_limit(0);
    
    # Make sure to keep alive the script when a client disconnect.
    ignore_user_abort(true);
    

    A client disconnection will interrupt the execution of the script and possibly damaging data,
    eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file... and you might, or not, care about this.

    Answering this old question because this thread is at the top on engine searches for CURL_TIMEOUT.

提交回复
热议问题