Php uses the curl concurrent method to reduce the backend access time analysis, curl backend _ PHP Tutorial

Source: Internet
Author: User
Tags how to use curl
Php uses curl for concurrent analysis to reduce the backend access time. Php uses the curl concurrent method to reduce the backend access time. the curl backend instance in this article describes how php uses the curl concurrent method to reduce the backend access time. For your reference, this article describes how to use curl in php to reduce the backend access time.

This example describes how php uses curl to concurrently reduce the backend access time. We will share this with you for your reference. The details are as follows:

In our usual programs, it is inevitable that we access several interfaces at the same time. when we use curl for access, it is generally a single, sequential access. if there are three interfaces, every interface takes 500 milliseconds, so it takes 1500 milliseconds for the three interfaces. this problem is too headache and seriously affects the page access speed. Is there any possibility of concurrent access to increase the speed? Today, we will briefly talk about how to use curl concurrency to speed up page access,

1. old curl access methods and time consumption statistics

<? Phpfunction curl_fetch ($ url, $ timeout = 3) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ data = curl_exec ($ ch); $ errno = curl_errno ($ ch); if ($ errno> 0) {$ data = false;} curl_close ($ ch); return $ data;} function microtime_float () {list ($ usec, $ sec) = explode ("", microtime (); return (float) $ usec + ( Float) $ sec);} $ url_arr = array ("taobao" => "http://www.taobao.com", "sohu" => "http://www.sohu.com ", "sina" => "http://www.sina.com.cn",); $ time_start = microtime_float (); $ data = array (); foreach ($ url_arr as $ key => $ val) {$ data [$ key] = curl_fetch ($ val);} $ time_end = microtime_float (); $ time = $ time_end-$ time_start; echo "time consumed: {$ time} ";?>

Duration: 0.614 seconds

2. curl concurrent access mode and time consumption statistics

<? Phpfunction curl_multi_fetch ($ urlarr = array () {$ result = $ res = $ ch = array (); $ nch = 0; $ mh = curl_multi_init (); foreach ($ urlarr as $ nk => $ url) {$ timeout = 2; $ ch [$ nch] = curl_init (); curl_setopt_array ($ ch [$ nch], array (CURLOPT_URL => $ url, CURLOPT_HEADER => false, response => true, CURLOPT_TIMEOUT => $ timeout,); curl_multi_add_handle ($ mh, $ ch [$ nch]); + + $ nch;}/* wait for faster Ming re Quest */do {$ mrc = curl_multi_exec ($ mh, $ running);} while (CURLM_CALL_MULTI_PERFORM = $ mrc); while ($ running & $ mrc = CURLM_ OK) {// wait for network if (curl_multi_select ($ mh, 0.5)>-1) {// pull in new data; do {$ mrc = curl_multi_exec ($ mh, $ running) ;}while (CURLM_CALL_MULTI_PERFORM ==$ mrc) ;}} if ($ mrc! = CURLM_ OK) {error_log ("CURL Data Error");}/* get data */$ nch = 0; foreach ($ urlarr as $ moudle => $ node) {if ($ err = curl_error ($ ch [$ nch]) = '') {$ res [$ nch] = curl_multi_getcontent ($ ch [$ nch]); $ result [$ moudle] = $ res [$ nch];} else {error_log ("curl error");} curl_multi_remove_handle ($ mh, $ ch [$ nch]); curl_close ($ ch [$ nch]); ++ $ nch;} curl_multi_close ($ mh); return $ result ;}$ url_arr = array ("taobao" =>" http://www.taobao.com "," Sohu "=>" http://www.sohu.com "," Sina "=>" http://www.sina.com.cn ",); Function microtime_float () {list ($ usec, $ sec) = explode (" ", microtime (); return (float) $ usec + (float) $ sec) ;}$ time_start = microtime_float (); $ data = curl_multi_fetch ($ url_arr); $ time_end = microtime_float (); $ time = $ time_end-$ time_start; echo "time consumed: {$ time}";?>

Duration: 0.316 seconds

Handsome, the time spent on accessing the backend interface on the entire page is halved.

3. curl-related parameters

Curl_close-Close a cURL session
Curl_copy_handle-Copy a cURL handle along with all of its preferences
Curl_errno-Return the last error number
Curl_error-Return a string containing the last error for the current session
Curl_exec-Perform a cURL session
Curl_getinfo-Get information regarding a specific transfer
Curl_init-Initialize a cURL session
Curl_multi_add_handle-Add a normal cURL handle to a cURL multi handle
Curl_multi_close-Close a set of cURL handles
Curl_multi_exec-Run the sub-connections of the current cURL handle
Curl_multi_getcontent-Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
Curl_multi_info_read-Get information about the current transfers
Curl_multi_init-Returns a new cURL multi handle
Curl_multi_remove_handle-Remove a multi handle from a set of cURL handles
Curl_multi_select-Wait for activity on any curl_multi connection
Curl_setopt_array-Set multiple options for a cURL transfer
Curl_setopt-Set an option for a cURL transfer
Curl_version-Gets cURL version information

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.