PHP uses curl concurrency to reduce back-end access time analysis, Curl backend _php Tutorial

Source: Internet
Author: User

PHP uses curl concurrency to reduce back-end access time analysis, Curl backend


The example in this article describes how PHP uses curl to reduce the back-end access time. Share to everyone for your reference, as follows:

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,

1. Old Curl access methods and time-consuming 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 _e nd-$time _start; echo "time consuming: {$time}";? >

Time: 0.614 seconds

2, Curl concurrent access mode and time-consuming 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, Curlopt_returntransfer =& Gt    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_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" and "http://www.sohu.com", "sina" and "="//  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}";? >

Time: 0.316 seconds

Handsome. The entire page accesses the backend interface for half the time saved

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

More about PHP related content readers can view the topic: "PHP Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Data structure and algorithm tutorial", "PHP Math Skills Summary", "PHP date and Time usage summary", " PHP Primer for object-oriented programming, PHP string Usage Summary, Introduction to PHP+MYSQL database operations, and PHP common database operations Tips Summary

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1125866.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125866.html techarticle PHP uses curl concurrency to reduce the back-end access time of the analysis, Curl backend This article describes the PHP using Curl concurrency to reduce the back-end access time method. Share to everyone for reference, with ...

  • Related Article

    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.