[Turn]curl_multi to implement a quasi-multi-process send request

Source: Internet
Author: User

From:http://blog.sina.com.cn/s/blog_515b90d00100jtnv.html

Curl_multi function Family:
Curl_multi_close
Curl_multi_exec
Curl_multi_getcontent
Curl_multi_add_handle
Curl_multi_info_read
Curl_multi_init
Curl_multi_remove_handle
Curl_multi_select

Usage steps:
First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
It is important to note that the second parameter of Curl_multi_add_handle is a sub-handle from Curl_init.
Step three: Continue calling Curl_multi_exec
Fourth step: Loop call Curl_multi_getcontent as needed to get results
Fifth step: Call Curl_multi_remove_handle, and call curl_close for each word handle
Sixth step: Call Curl_multi_close

Let's look at my use in a small project:

   Public functionRequest ($requestdata,$kw)    {         $MH=Curl_multi_init (); $running=NULL; $user _agent= "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "; foreach($requestdata  as&$data)        {            $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$data[' URL ']); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_useragent,$user _agent); curl_setopt ($ch, Curlopt_timeout, 100000); Curl_multi_add_handle ($MH,$ch); $data[' ch ']=$ch; $data[' Start ']=Microtime(1); }unset($data);  Do{             while(($execrun= Curl_multi_exec ($MH,$running)) ==curlm_call_multi_perform);  while($done= Curl_multi_info_read ($MH)) {                foreach($requestdata  as $data)                {                    if($data[' ch '] = = = =$done[' Handle ']) {                        $html= Curl_multi_getcontent ($done[' Handle ']); Call_user_func($data[' Callback '],$data[' URL '],$html,round((Microtime(1)-$data[' Start ']), 2),$kw); Curl_multi_remove_handle ($MH,$done[' Handle ']); }                }            }            if(!$running) Break;  while(($res= Curl_multi_select ($MH)) = = = 0); if($res===false) Break; } while(true); Curl_multi_close ($MH); }

[Turn]curl_multi to implement a quasi-multi-process send request

Related Article

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.