Using Curl concurrency to improve page access Speed _php Tutorial

Source: Internet
Author: User
In our usual program will inevitably appear at the same time access to several interfaces, usually we use curl to access the time, generally is a single, sequential access, if there are 3 interfaces, each interface takes 500 milliseconds then we three interface will cost 1500 milliseconds, This problem is too headache to seriously affect the page access speed, there is no possibility of concurrent access to improve speed? Let's just say today, using curl concurrency to improve page access speed,
I hope you have more guidance.
1. Old Curl access methods and time-consuming statistics

function 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 _e nd-$time _start; echo "time consuming: {$time}";? >

  



Time: 0.614 seconds
2, Curl concurrent access mode and time-consuming statistics

  $url) {$timeout = 2;        $ch [$nch] = Curl_init (); Curl_setopt_array ($ch [$nch], array (curlopt_url = $url, Curlopt_header = False, Curlopt_retur        Ntransfer = true, Curlopt_timeout = $timeout,));        Curl_multi_add_handle ($MH, $ch [$nch]);    + + $nch;    }/* Wait for performing request */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_mul Ti_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" and "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 consuming: {$time}";? >

Output result Array (' Taobao ' =>result, ' Sina ' =>result, ' Baidu ' =>result);

Time: 0.316 seconds
Handsome. The entire page accesses the backend interface for half the time saved
3. Curl Related Parameters
From: http://cn2.php.net/manual/en/ref.curl.php
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

Original address: Http://www.searchtb.com/2010/12/using-multicurl-to-improve-performance.html?spm=0.0.0.0.onjnnY

http://www.bkjia.com/PHPjc/749284.html www.bkjia.com true http://www.bkjia.com/PHPjc/749284.html techarticle in our usual program will inevitably appear at the same time access to several interfaces, usually we use curl to access the time, generally is a single, sequential access, if there are 3 interfaces, each ...

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