PHP Curl Set timeout instance _php tips

Source: Internet
Author: User

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

There are many ways to access HTTP, you can use curl, sockets, file_get_contents () and other methods.
When you are accessing HTTP, you need to consider the problem of timeouts.

Curl Access http:

CURL is a common Lib library to access HTTP protocol interface, which has high performance and some concurrent support functions.
curl_setopt ($ch, opt) can set some time-out settings, mainly including:
① (important) Curlopt_timeout sets the maximum number of seconds that the curl allows to execute.
② (important) Curlopt_timeout_ms sets the maximum number of milliseconds that the curl allows to execute.
(Joined in the Curl 7.16.2.) Available from PHP 5.2.3)
③curlopt_connecttimeout the time to wait before initiating the connection, and if set to 0, wait indefinitely.
④curlopt_connecttimeout_ms the time, in milliseconds, that the attempt to connect waits.  If set to 0, wait indefinitely. (Joined in the 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 Ordinary Second level timeout:

Copy 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 just need to set a number of seconds to
curl_setopt ($ch, Curlopt_httpheader, $headers);
curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent '));

2. Curl Ordinary Second level timeout use:

Copy Code code as follows:
curl_setopt ($ch, curlopt_timeout,60);

3. Curl needs to be increased if a millisecond timeout is required:

Copy Code code 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 help you with your PHP program design.

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.