Home>Article>Backend Development> How to set php curl timeout

How to set php curl timeout

藏色散人
藏色散人 Original
2020-07-28 10:49:02 10698browse

php curl timeout setting method: 1. Use "curl_setopt($ch, opt)" to set some timeouts; 2. Use "CURLOPT_DNS_CACHE_TIMEOUT" to set the time to save DNS information in memory.

How to set php curl timeout

Recommended: "PHP Tutorial"

in php When using curl, the detailed method of setting timeout

There are many ways to access HTTP, you can use curl, socket, file_get_contents() and other methods.

When accessing http, you need to consider the timeout issue.

1. CURL accesses HTTP

CURL is a commonly used lib library for accessing the HTTP protocol interface. It has high performance and some concurrent support functions.

curl_setopt($ch, opt) You can set some timeout settings, mainly including:

*(Important) CURLOPT_TIMEOUT sets the maximum number of seconds that cURL is allowed to execute.

*(Important) CURLOPT_TIMEOUT_MS sets the maximum number of milliseconds that cURL is allowed to execute.

(Added in cURL 7.16.2. Available from PHP 5.2.3)

CURLOPT_CONNECTTIMEOUT The time to wait before initiating a connection. If set to 0, it will wait infinitely.

CURLOPT_CONNECTTIMEOUT_MS The time to wait for a connection attempt, in milliseconds. If set to 0, wait infinitely.

(Added in cURL 7.16.2. Available since PHP 5.2.3)

CURLOPT_DNS_CACHE_TIMEOUT Set the time to save DNS information in memory, the default is 120 seconds.

1, curl ordinary second-level timeout:

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,60); //只需要设置一个秒的数量就可以 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

2, curl ordinary second-level timeout use:

curl_setopt($ch, CURLOPT_TIMEOUT,60);

3, if curl needs millisecond timeout, you need to add:

curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L); //或者 curl_setopt ( $ch, CURLOPT_NOSIGNAL,true);//支持毫秒级别超时设置

Example of curl timeout setting.

1, curl a millisecond timeout example:

0){ echo "cURL Error ($curl_errno): $curl_error\n"; }else{ echo "Data received: $data\n"; } }else{ // Server sleep(10); echo "Done."; }

Tips:

1, cURL version >= libcurl/7.21.0 version, millisecond timeout is certain Effective, remember.

2. Curl_multi’s millisecond-level timeout problem. A single access supports ms-level timeout. Curl_multi’s parallel adjustment will be inaccurate.

2. Access HTTP through stream processing

In addition to curl, fsockopen or file operation functions are often used to process the HTTP protocol.

Let’s talk about the timeout setting in this regard.

Generally the connection timeout can be set directly, but the stream read timeout needs to be handled separately.

You can refer to the following implementation code:

 $this->_intReadTimeoutUS){ returnfalse; }

Or use the built-in stream processing functions stream_set_timeout() and stream_get_meta_data() to process:


      

file_get_contents timeout:

 array( 'timeout'=>5//设置一个超时时间,单位为秒 ) ); $ctx = stream_context_create($timeout); $text = file_get_contents("http://example.com/",0, $ctx);

fopen timeout:

 array( 'timeout' => 5 //设置一个超时时间,单位为秒 ) ); $ctx = stream_context_create($timeout); if ($fp = fopen("http://example.com/", "r", false, $ctx)) { while( $c = fread($fp, 8192)) { echo $c; } fclose($fp); }

The above is the detailed content of How to set php curl timeout. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn