Home >Backend Development >PHP Tutorial >Analysis of how PHP uses curl concurrency to reduce backend access time
this article will introduce to you how php uses curl concurrency to reduce back-end access time. combined with examples, it will comparatively analyze the program running time before and after curl uses concurrent access. it has certain reference value. friends in need can refer to it. i hope it will be helpful to everyone.
in our daily programs, it is inevitable to access several interfaces at the same time. when we usually use curl to access, we usually access them individually and sequentially. if there are 3 interfaces, each interface takes 500 milliseconds, so our three interfaces will take 1500 milliseconds. this problem is too headache and seriously affects the page access speed. is it possible to increase the speed by concurrent access? today i will briefly talk about using curl concurrency to improve page access speed.
1. old curl access method 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}"; ?>
time-consuming: 0.614 seconds
2. curl concurrent access method 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}"; ?>
time consuming: 0.316 seconds
the entire page of handsome bar saves half the time to access the back-end interface
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
i hope this article will be helpful to everyone in php programming.
recommended learning: "php video tutorial》