PHP curl proxy captures data

Source: Internet
Author: User
<? Phpdefine ('is _ proxy', true); // whether to enable PROXY function async_get_url ($ url_array, $ wait_usec = 0) {if (! Is_array ($ url_array) return false; $ wait_usec = intval ($ wait_usec); $ data = array (); $ handle = array (); $ running = 0; $ mh = curl_multi_init (); // enable multithreading $ I = 0; foreach ($ url_array as $ url) {$ ch = curl_init (); if (IS_PROXY) {// The following Code sets the proxy server address http://www.cnproxy.com/proxy1.html !! Hong Kong and China have a better speed than curl_setopt ($ ch, CURLOPT_PROXY, '2017. 4.12.170: 80');} curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return don't print curl_setopt ($ ch, CURLOPT_TIMEOUT, 30); // set the timeout value curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect curl_setopt ($ ch, CUR LOPT_MAXREDIRS, 7); // HTTp Targeting level curl_multi_add_handle ($ mh, $ ch ); // put curl resource into multi curl handler $ handle [$ I ++] = $ ch;}/* execute */do {$ mrc = curl_multi_exec ($ mh, $ running); if ($ wait_usec> 0)/* how long each connect interval */usleep ($ wait_usec ); // 250000 = 0.25 sec} while ($ mrc = CURLM_CALL_MULTI_PERFORM); while ($ running & $ mrc = CURLM_ OK) {if (curl_multi_select ($ mh )! =-1) {do {$ mrc = curl_multi_exec ($ mh, $ running);} while ($ mrc = CURLM_CALL_MULTI_PERFORM );}} /* read data */foreach ($ handle as $ I => $ ch) {$ content = curl_multi_getcontent ($ ch ); $ data [$ I] = (curl_errno ($ ch) = 0 )? $ Content: false;}/* remove handle */foreach ($ handle as $ ch) {curl_multi_remove_handle ($ mh, $ ch);} curl_multi_close ($ mh ); return $ data ;}$ urls = array ('HTTP: // map.baidu.com '); $ re = async_get_url ($ urls); echo $ re [0];?>

From: http://hi.baidu.com/ylj798/item/322468136b70bc433a176efc

Http://www.oschina.net/code/snippet_80475_6689

Curl entry: http://www.chinaz.com/program/2010/0119/104346_5.shtml

Free Proxy: http://www.cnproxy.com/proxy1.html

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.