Php curl setting timeout instance, phpcurl instance

Source: Internet
Author: User
Tags apache error log

Php curl setting timeout instance, phpcurl instance

This example describes how to set curl timeout in php. Share it with you for your reference. The specific implementation method is as follows:

There are many HTTP access methods. You can use curl, socket, file_get_contents () and other methods.
Timeout must be considered when accessing http.

CURL access HTTP:

CURL is a common lib library used to access HTTP interfaces. It features high performance and support for concurrency.
Curl_setopt ($ ch, opt) can be used to set timeout settings, including:
① (Important) CURLOPT_TIMEOUT sets the maximum number of seconds that cURL can be executed.
② (Important) CURLOPT_TIMEOUT_MS sets the maximum number of milliseconds that cURL can be executed.
(Added in cURL 7.16.2. Available from PHP 5.2.3)
③ CURLOPT_CONNECTTIMEOUT: the waiting time before the connection is initiated. If it is set to 0, the waiting time is unlimited.
④ The waiting time for CURLOPT_CONNECTTIMEOUT_MS to connect, in milliseconds. If it is set to 0, the system waits for no limit. (Added in cURL 7.16.2. Available from PHP 5.2.3)
⑤ CURLOPT_DNS_CACHE_TIMEOUT sets the time for saving DNS information in the memory. The default value is 120 seconds.
 
1. curl common seconds Timeout:
Copy codeThe 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); // you only need to set the number of seconds.
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers );
Curl_setopt ($ ch, CURLOPT_USERAGENT, $ defined_vars ['HTTP _ USER_AGENT ']);

2. curl common timeout in seconds:
Copy codeThe Code is as follows: curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );

3. curl:
Copy codeThe Code is as follows: curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L );
// Or
Curl_setopt ($ ch, CURLOPT_NOSIGNAL, true); // supports millisecond-level timeout settings
 
I hope this article will help you with PHP programming.


Several common PHP timeout Methods

Web Server timeout Processing]

[Apache]
Generally, when the performance is high, all timeout configurations are 30 seconds by default. However, when the file is uploaded or the network speed is very slow, the timeout operation may be triggered.
Currently, apachefastcgiphp-fpm mode has three timeout settings:
Fastcgi timeout settings:
The modified fastcgi connection configuration is similar to the following:

Copy the Code as follows:

<IfModulemod_fastcgi.c>
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 30 s. If you need to customize your own configuration, You need to modify the configuration, for example, to 100 s: (restart apache after modification ):

Copy the Code as follows:

<IfModulemod_fastcgi.c>
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-
</IfModule>

If the timeout occurs, a 500 error is returned. 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 (0 bytes) receivedfromserver "/home/forum/apache/apache_php/cgi-bin/php-cgi"
Other 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.