Curl non-blocking call class [PHP]

Source: Internet
Author: User

I have written an article about non-blocking PHP calls.ArticlePHP implements the non-blocking mode. In fact, if it is only HTTP, it can be implemented directly using curl.

OnlineCodeAfter modification, a non-blocking call class that supports post/get is encapsulated.

Test the bug ~~~~~

/*************************************** * ************* <Br/> curl non-blocking call class <br/> auther: linvo <br/> copyright (c) 2010/10/21 <br/> *********************************** * ******************/<br/>/* <br/> // example <br/>/ /input parameter description <br/> // URL request address <br/> // data POST method data <br/> // concurrent call <br/> $ param1 = array (<br/> array (<br/> 'url' => "http: // localhost/. PHP? S = 1 ", <br/>), <br/> array (<br/> 'url' =>" http: // localhost/a. php? S = 1 ", <br/> 'data' => array ('aaa' => 1, 'bbb '=> 2), <br/> ), <br/>); </P> <p> // single call <br/> $ param2 = array (<br/> 'url' => "http: // localhost/. PHP? S = 0 ", <br/> 'data' => array ('aaa' => 1, 'bbb '=> 2), <br/> ); </P> <p> // single call (simple get method) <br/> $ param3 = 'HTTP: // localhost/. PHP? S = 2'; <br/> $ AC = new asynccurl (); <br/> $ ac-> set_param ($ param1 ); <br/> $ ret = $ ac-> send (); <br/> // The returned value is an array of results in the order of request parameters (the element value is false, indicating a request error) <br/> var_dump ($ RET ); <br/> */</P> <p> class asynccurl <br/> {<br/>/** <br/> * whether to return HTTP header information <br /> */<br/> Public $ curlopt_header = 0; <br/>/** <br/> * single curl call timeout limit <br/> */<br/> Public $ curlopt_timeout = 20; <br/> private $ Param = array (); <br/>/** <br/> * Constructor (you can directly Input Request Parameters) <br/> * @ Param array (optional) <br/> * @ return void <br/> */<br/> public function _ construct ($ Param = false) <br/>{< br/> if ($ Param! = False) <br/>{< br/> $ this-> param = $ this-> init_param ($ PARAM ); <br/>}< br/>/** <br/> * Set Request Parameters <br/> * @ Param array <br /> * @ return void <br/> */<br/> Public Function set_param ($ PARAM) <br/>{< br/> $ this-> param = $ this-> init_param ($ PARAM ); <br/>}</P> <p>/** <br/> * send a request <br/> * @ return array <br/> * /<br/> Public Function send () <br/>{< br/> If (! Is_array ($ this-> param) |! Count ($ this-> param) <br/>{< br/> return false; <br/>}< br/> $ curl = $ ret = array (); <br/> $ handle = curl_multi_init (); <br/> foreach ($ this-> param as $ k => $ V) <br/>{< br/> $ Param = $ this-> check_param ($ V); <br/> If (! $ PARAM) $ curl [$ K] = false; <br/> else $ curl [$ K] = $ this-> add_handle ($ handle, $ PARAM ); <br/>}< br/> $ this-> exec_handle ($ handle); <br/> foreach ($ this-> param as $ k => $ V) <br/>{< br/> if ($ curl [$ K]) <br/>{< br/> $ RET [$ K] = curl_multi_getcontent ($ curl [$ K]); <br/> curl_multi_remove_handle ($ handle, $ curl [$ K]); <br/>}else {<br/> $ RET [$ K] = false; <br/>}< br/> curl_multi_close ($ handle); <br /> Return $ ret; <br/>}< br/> // Private method <br/> private function init_param ($ PARAM) <br/>{< br/> $ ret = false; <br/> If (isset ($ Param ['url']) <br/>{< br/> $ ret = array ($ PARAM ); <br/>} else {<br/> $ ret = isset ($ Param [0])? $ Param: false; <br/>}< br/> return $ ret; <br/>}< br/> private function check_param ($ Param = array ()) <br/>{< br/> $ ret = array (); <br/> If (is_string ($ PARAM )) <br/>{< br/> $ url = $ Param; <br/>}else {<br/> extract ($ PARAM ); <br/>}< br/> If (isset ($ URL) <br/>{< br/> $ url = trim ($ URL ); <br/> $ url = stripos ($ URL, 'HTTP: // ') === 0? $ URL: NULL; <br/>}< br/> If (isset ($ data) & is_array ($ data )&&! Empty ($ data) <br/>{< br/> $ method = 'post'; <br/>}else {<br/> $ method = 'get '; <br/> unset ($ data); <br/>}< br/> If (isset ($ URL) $ RET ['url'] = $ URL; <br/> If (isset ($ method) $ RET ['method'] = $ method; <br/> If (isset ($ data )) $ RET ['data'] = $ data; <br/> $ ret = isset ($ URL )? $ RET: false; <br/> return $ ret; <br/>}< br/> private function add_handle ($ handle, $ PARAM) <br/>{< br/> $ curl = curl_init (); <br/> curl_setopt ($ curl, curlopt_url, $ Param ['url']); <br/> curl_setopt ($ curl, curlopt_header, $ this-> curlopt_header); <br/> curl_setopt ($ curl, curlopt_returntransfer, 1 ); <br/> curl_setopt ($ curl, curlopt_timeout, $ this-> curlopt_timeout); <br/> if ($ Param ['method'] = 'post ') <br/>{< br/> curl_setopt ($ curl, curlopt_post, 1); <br/> curl_setopt ($ curl, curlopt_postfields, $ Param ['data']); <br/>}< br/> curl_multi_add_handle ($ handle, $ curl); <br/> return $ curl; <br/>}< br/> private function exec_handle ($ handle) <br/>{< br/> $ flag = NULL; <br/> do {<br/> curl_multi_exec ($ handle, $ flag); <br/>}while ($ flag> 0 ); <br/>}< br/>} 

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.