Home >Backend Development >PHP Tutorial >php curl setting timeout example, phpcurl example_PHP tutorial

php curl setting timeout example, phpcurl example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:301760browse

php curl setting timeout example, phpcurl example

The example in this article describes the curl timeout setting method in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

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 issue of timeout.

CURL access HTTP:

CURL is a commonly used lib library for accessing HTTP protocol interfaces. It has high performance and has some concurrency support functions.
curl_setopt($ch, opt) 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 onwards)
③ CURLOPT_CONNECTTIMEOUT is the time to wait before initiating a connection. If set to 0, it will wait indefinitely.
④ CURLOPT_CONNECTTIMEOUT_MS The time to wait for trying to connect, in milliseconds. If set to 0, wait infinitely. (Added in cURL 7.16.2. Available since PHP 5.2.3)
⑤ CURLOPT_DNS_CACHE_TIMEOUT sets the time to save DNS information in memory, the default is 120 seconds.

1. curl normal second-level timeout:

Copy code The code is as follows:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,60); //Just set the number of seconds
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);


2. Curl normal second-level timeout use:


Copy code

The code is as follows: curl_setopt($ch, CURLOPT_TIMEOUT,60);
3. If curl requires millisecond timeout, you need to increase:


Copy code

The code is as follows: curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L); //or  curl_setopt ($ch, CURLOPT_NOSIGNAL,true);//Support millisecond level timeout setting

I hope this article will be helpful to everyone’s PHP programming.


Several common PHP timeout handling methods

[Web server timeout processing]

[ Apache ]
Generally, when the performance is very high, all default timeout configurations are 30 seconds, but when uploading files, or the network speed is very slow In this case, a timeout operation may be triggered.
There are currently three timeout settings in apachefastcgiphp-fpm mode:
fastcgi timeout setting:
Modified fastcgi connection configuration, similar to the following:

Copy the code as follows:


FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock
ScriptAlias/fcgi-bin /"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-
< /IfModule>

The default configuration is 30s. If you need to customize your own configuration, you need to modify the configuration, for example, to 100 seconds: (Restart apache after modification):

Copy the code as follows:

 
FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock-idle -timeout100
ScriptAlias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-


If it times out, it will return a 500 error, disconnect from the backend php service, and record an apache error log:
[ThuJan2718:30 :152011][error][client10.81.41.110]FastCGI:commwithserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"aborted:idletimeout(30sec)
[ThuJan2718:30:152011] [error][client10.81.41.110]FastCGI:incompleteheaders(0bytes)receivedfromserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"
Other fastcgi...the rest of the full text>>

curl timeout output

function curl_file_get_contents($url){
$starttime = time();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = trim(curl_exec($ch)) ;
curl_close($ch);

$endtime = time();

if($endtime-$starttime>=15){
echo 'php running timeout' ;
}

return $contents;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904928.htmlTechArticlephp curl setting timeout example, phpcurl example This article describes the curl timeout setting method in php. Share it with everyone for your reference. The specific implementation method is as follows: There are many ways to access HTTP,...
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