Curl batch processing from the official demo package

Source: Internet
Author: User
Tags usleep

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

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.