PHP Curl Set Timeout instance, phpcurl instance _php tutorial

Source: Internet
Author: User
Tags php programming

PHP Curl Set Timeout instance, phpcurl instance


The example in this article describes the Curl Timeout setting method in PHP. Share to everyone for your reference. The implementation method is as follows:

There are many ways to access HTTP, such as curl, Socket, file_get_contents ().
When you access HTTP, you need to consider the issue of timeouts.

Curl accesses http:

CURL is a common Lib library that accesses the HTTP protocol interface, with high performance, and some features that are supported concurrently.
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 allows to execute.
(added in Curl 7.16.2.) Available from PHP 5.2.3)
③curlopt_connecttimeout the time to wait before initiating the connection, or infinite wait if set to 0.
④curlopt_connecttimeout_ms the time, in milliseconds, that the connection is trying to wait. If set to 0, the wait is infinite. (added in Curl 7.16.2.) Available starting from PHP 5.2.3)
⑤curlopt_dns_cache_timeout sets the time to save DNS information in memory by default of 120 seconds.

1. Curl Normal second timeout:
Copy the Code code 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 to
curl_setopt ($ch, Curlopt_httpheader, $headers);
curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent ');

2. Curl normal second time-out use:
Copy the Code code as follows: curl_setopt ($ch, curlopt_timeout,60);

3. If you need to perform a millisecond timeout, curl needs to increase:
Copy the Code code as follows: Curl_easy_setopt (curl, curlopt_nosignal,1l);
Or
curl_setopt ($ch, curlopt_nosignal,true);//support for millisecond-level timeout settings

I hope this article is helpful to everyone's PHP programming.


Several common methods of PHP timeout processing

"Web Server timeout Processing"

[Apache]
Generally in high performance situations, the default all timeout configuration is 30 seconds, but in the case of uploading files, or network speed is very slow, it is possible to trigger a timeout operation.
There are currently three timeout settings in APACHEFASTCGIPHP-FPM mode:
FastCGI Timeout setting:
Modify the fastcgi connection configuration, similar to the following:

Copy the code 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 configuration, you need to modify the configuration, such as modified to 100 seconds: (restart Apache after modification):

Copy the code 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-
</IfModule>

If the timeout returns a 500 error, disconnect the back-end PHP service and log an Apache error logs:
[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 ... Remaining 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 runtime timeout ';
}

return $contents;
}

http://www.bkjia.com/PHPjc/904928.html www.bkjia.com true http://www.bkjia.com/PHPjc/904928.html techarticle PHP Curl Set timeout instance, phpcurl instance This article describes the Curl Timeout setting method in PHP. Share to everyone for your reference. The implementation method is as follows: There are many ways to access HTTP, ...

  • 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.