Example of a curl batch implemented in PHP php curl download php curl.dll download php Curl simulator

Source: Internet
Author: User
Curl is an open source file Transfer tool that works with URL syntax in command line mode

This article implements a Curl batch instance in PHP.

The code is as follows:

1Header("Content-type:text/html;charset=utf8");23/*get all a labels for two pages first*/4//Initialize two simple handle handles5$ch 1=curl_init ();6$ch 2=curl_init ();7Curl_setopt_array ($ch 1,Array(8Curlopt_url = ' http://www.sina.com.cn ',9Curlopt_header = 0,TenCurlopt_returntransfer = 1, One)); ACurl_setopt_array ($ch 2,Array( -Curlopt_url = ' http://www.baidu.com/', -Curlopt_header = 0, theCurlopt_returntransfer = 1, -)); - -//Initializes a batch handle, and adds a simple handle to the processing handle +$MH=curl_multi_init (); -Curl_multi_add_handle ($MH,$ch 1); +Curl_multi_add_handle ($MH,$ch 2); A at//Initialize execution State -$state=NULL; - -//perform batch processing - Do{ -$MC= Curl_multi_exec ($MH,$state);  in} while($MC==curlm_call_multi_perform); - while($MC= = Curlm_ok &&$state) { to while(Curl_multi_exec ($MH,$state) ===curlm_call_multi_perform); +//after an experiment, it was found that curl_multi_select ($MH) always returns 1, meaning that the code will not execute -if(Curl_multi_select ($MH)! =-1) { the Do{ *$MC= Curl_multi_exec ($MH,$state); $} while($MC==curlm_call_multi_perform);Panax Notoginseng    }     -} the +//Get content A$text= Curl_multi_getcontent ($ch 1); the$text. = Curl_multi_getcontent ($ch 2); + -//Find all the A tags on the page and save to $matches $$matches=NULL; $Preg_match_all("/(.*?) <\/a>/",$text,$matches); - -//close each handle theCurl_multi_remove_handle ($MH,$ch 1); -Curl_multi_remove_handle ($MH,$ch 2);WuyiCurl_multi_close ($MH); the -/*continue to find the title tag in the found connection*/ Wu -$handle=Array();//arrays that store simple handle handles About$mhandle= Curl_multi_init ();//Batch handle $//Processing 100 pages -foreach(Array_slice($matches[1],0,100] as$href) { -$tmp _h=curl_init (); -Curl_setopt_array ($tmp _h,Array( ACurlopt_url =$href, +Curlopt_header = 0, theCurlopt_returntransfer = 1, -    )); $Curl_multi_add_handle ($mhandle,$tmp _h); the$handle[] =$tmp _h; the} the Do{ the$MRC= Curl_multi_exec ($mhandle,$active); -} while($MRC==curlm_call_multi_perform); in while($MRC= = Curlm_ok &&$active) { the while(Curl_multi_exec ($mhandle,$active) ==curlm_call_multi_perform); theif(Curl_multi_select ($mhandle)! =-1) { About Do{ the$MRC= Curl_multi_exec ($mhandle,$active); the} while($MRC==curlm_call_multi_perform); the    } +} - the//get the contents of these pagesBayi$mtext=NULL; theforeach($handle as$tmp _h) { the$mtext. = Curl_multi_getcontent ($tmp _h); -Curl_multi_remove_handle ($mhandle,$tmp _h); -} the$mmatches=Array(); thePreg_match_all("/<title>(.*?) <\/title>/",</title>$mtext,$mmatches); the the//Encoding Conversion -Mb_detect_order (' Gb2312,gbk,big5,gb18030,unicode, CP936 '); theforeach($mmatches[1] as$key=$val) { the$encoding= Mb_detect_encoding ($val); theif($encoding! = ' UTF-8 ' &&$encoding! = ' CP936 ' &&$encoding! = ' GB18030 ' &&$encoding!='') {94$mmatches[1] [$key] =Iconv($encoding, ' Utf-8//ignore ',$val); the    } the} the98//Print title Information AboutVar_dump($mmatches[1]); -101//close a batch handle102Curl_multi_close ($mhandle);

The above is introduced in PHP implementation of a Curl batch of examples, including the curl,php aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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