Php curl setting timeout instance

Source: Internet
Author: User
This article mainly introduces the curl setting timeout method in php. The example describes various timeout setting methods in curl, which is very useful. For more information, see

This article mainly introduces the curl setting timeout method in php. The example describes various timeout setting methods in curl, which is very useful. For more information, see

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:

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); // 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:

The Code is as follows:

Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );

3. curl:

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

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.