This article illustrates how PHP uses curl concurrency to reduce backend access time. Share to everyone for your reference, specific as follows:
In our usual program will inevitably appear at the same time access to several interfaces, usually when we use curl to visit, is generally a single, sequential access, if there are 3 interfaces, each interface takes 500 milliseconds then our three interfaces will cost 1500 milliseconds, This problem is too painful to seriously affect the page access speed, there is no possibility of concurrent access to improve speed? Today, simply speaking, using curl concurrency to improve page access speed,
1, the old Curl access mode and time-consuming statistics
<?php
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 _end-$time _start;
echo "time consuming: {$time}";
? >
Time consuming: 0.614 seconds
2, Curl concurrent access mode and time-consuming statistics
<?php function 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
=> 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" => "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 consuming: {$time}";?>
Time consuming: 0.316 seconds
It's cool. The entire page accesses back-end interfaces at 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
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 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 Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Data structure and algorithm tutorial", "PHP Mathematical Calculation Skills Summary", "PHP date and Time usage summary", " Introduction to PHP object-oriented programming, "PHP string (String) Usage Summary", "Php+mysql Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"
I hope this article will help you with the PHP program design.