PHP uses curl to reduce the time required to obtain third-party webpage content concurrently-PHP source code

Source: Internet
Author: User
PHP uses curl to concurrently reduce the time required to obtain third-party webpage content. in our ordinary programs, it is inevitable that we access several interfaces at the same time. when we use curl for access, generally, it is a single and sequential access. if there are three interfaces, each of which takes 500 milliseconds, it will take 1500 milliseconds for the three interfaces, this problem is too headache, seriously affecting the page access speed. Is it possible to improve the speed of concurrent access? Paste the code here and you will be able to see it at a glance.

1. old curl access methods and time consumption statistics

 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", "lai18" => "http://www.lai18.com",); $ 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}";?>

2. curl concurrent access mode and time consumption statistics

 $ 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 future Ming 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 consumed: {$ time}";?>

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.