Official demo
//Create a pair of curl resources$ch 1=curl_init ();$ch 2=curl_init ();//set the URL and the appropriate optionscurl_setopt ($ch 1, Curlopt_url, "http://www.example.com/"); curl_setopt ($ch 1, Curlopt_header, 0); curl_setopt ($ch 2, Curlopt_url, "http://www.php.net/"); curl_setopt ($ch 2, Curlopt_header, 0);//Create a batch curl handle$MH=curl_multi_init ();//Add 2 handlesCurl_multi_add_handle ($MH,$ch 1); Curl_multi_add_handle ($MH,$ch 2);$running=NULL;//executing a batch handle Do { Usleep(10000); Curl_multi_exec ($MH,$running);} while($running> 0);//Close all handlesCurl_multi_remove_handle ($MH,$ch 1); Curl_multi_remove_handle ($MH,$ch 2); Curl_multi_close ($MH);
First step Encapsulation--method
functionMulti_post ($urls){ //Create a batch curl handle $MH=Curl_multi_init (); //Create a pair of curl resources//set corresponding Options//Add handle foreach($urls as $key=$url) { $handles[$key] = Curl_init ($url); curl_setopt ($handles[$key], Curlopt_header, 0); Curl_multi_add_handle ($MH,$handles[$key]); } $running=NULL; //executing a batch handle Do { Usleep(10000); Curl_multi_exec ($MH,$running); } while($running> 0); //Close all handles foreach($handles as $key=$handle) {Curl_multi_remove_handle ($MH,$handle); } curl_multi_close ($MH);}
$urls = Array (' http://www.shouji.com ', ' http://www.baidu.com ');
Multi_post ($urls);
Curl batch processing from the official demo package