{"id":477,"date":"2010-07-21T21:43:14","date_gmt":"2010-07-21T21:43:14","guid":{"rendered":""},"modified":"2010-07-21T21:43:14","modified_gmt":"2010-07-21T21:43:14","slug":"477","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=477","title":{"rendered":"PHP\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6293\u53d6\u7f51\u9875"},"content":{"rendered":"<p>PHP \u5229\u7528 Curl Functions \u53ef\u4ee5\u5b8c\u6210\u5404\u79cd\u4f20\u9001\u6587\u4ef6\u64cd\u4f5c\uff0c\u6bd4\u5982\u6a21\u62df\u6d4f\u89c8\u5668\u53d1\u9001GET\uff0cPOST\u8bf7\u6c42\u7b49\u7b49\uff0c\u53d7\u9650\u4e8ephp\u8bed\u8a00\u672c\u8eab\u4e0d\u652f\u6301\u591a\u7ebf\u7a0b\uff0c\u6240\u4ee5\u5f00\u53d1\u722c\u866b\u7a0b\u5e8f\u6548\u7387\u5e76\u4e0d\u9ad8\uff0c\u8fd9\u65f6\u5019\u5f80\u5f80\u9700 \u8981\u501f\u52a9Curl Multi Functions \u5b83\u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u591a\u7ebf\u7a0b\u7684\u8bbf\u95ee\u591a\u4e2aurl\u5730\u5740\u3002\u65e2\u7136 Curl Multi Function\u5982\u6b64\u5f3a\u5927\uff0c\u80fd\u5426\u7528 Curl Multi Functions \u6765\u5199\u5e76\u53d1\u591a\u7ebf\u7a0b\u4e0b\u8f7d\u6587\u4ef6\u5462\uff0c\u5f53\u7136\u53ef\u4ee5\uff0c\u4e0b\u9762\u7ed9\u51fa\u6211\u7684\u4ee3\u7801\uff1a<\/p>\n<p>\u4ee3\u78011\uff1a\u5c06\u83b7\u5f97\u7684\u4ee3\u7801\u76f4\u63a5\u5199\u5165\u67d0\u4e2a\u6587\u4ef6<\/p>\n<p><code><br \/>&lt;?php<br \/>$urls = array(&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.sina.com.cn\/\" target=\"_blank\" rel=\"external\">http:\/\/www.sina.com.cn\/<\/a>',&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.sohu.com\/\" target=\"_blank\" rel=\"external\">http:\/\/www.sohu.com\/<\/a>',&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.163.com\/\" target=\"_blank\" rel=\"external\">http:\/\/www.163.com\/<\/a>'&nbsp;&nbsp;<br \/>); \/\/ \u8bbe\u7f6e\u8981\u6293\u53d6\u7684\u9875\u9762URL&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>$save_to='\/test.txt';&nbsp;&nbsp; \/\/ \u628a\u6293\u53d6\u7684\u4ee3\u7801\u5199\u5165\u8be5\u6587\u4ef6&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>$st = fopen($save_to,\"a\");&nbsp;&nbsp; <br \/>$mh = curl_multi_init();&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>foreach ($urls as $i =&gt; $url) {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;$conn[$i] = curl_init($url);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_USERAGENT, \"Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 6.0)\");&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_HEADER ,0);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,60);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_FILE,$st); \/\/ \u8bbe\u7f6e\u5c06\u722c\u53d6\u7684\u4ee3\u7801\u5199\u5165\u6587\u4ef6&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_add_handle ($mh,$conn[$i]);&nbsp;&nbsp; <br \/>} \/\/ \u521d\u59cb\u5316&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>do {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_exec($mh,$active);&nbsp;&nbsp; <br \/>} while ($active);&nbsp;&nbsp;\/\/ \u6267\u884c&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>foreach ($urls as $i =&gt; $url) {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_remove_handle($mh,$conn[$i]);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_close($conn[$i]);&nbsp;&nbsp; <br \/>} \/\/ \u7ed3\u675f\u6e05\u7406&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>curl_multi_close($mh);&nbsp;&nbsp; <br \/>fclose($st); <br \/>?&gt;<br \/><\/code><\/p>\n<p>\u4ee3\u78012\uff1a\u5c06\u83b7\u5f97\u7684\u4ee3\u7801\u5148\u653e\u5165\u53d8\u91cf\uff0c\u518d\u5199\u5165\u67d0\u4e2a\u6587\u4ef6<\/p>\n<p><code><br \/>&lt;?php<br \/>$urls = array(&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.sina.com.cn\/\" target=\"_blank\" rel=\"external\">http:\/\/www.sina.com.cn\/<\/a>',&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.sohu.com\/\" target=\"_blank\" rel=\"external\">http:\/\/www.sohu.com\/<\/a>',&nbsp;&nbsp; <br \/> '<a href=\"http:\/\/www.163.com\/\" target=\"_blank\" rel=\"external\">http:\/\/www.163.com\/<\/a>'&nbsp;&nbsp;<br \/>);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;<br \/>$save_to='\/test.txt';&nbsp;&nbsp; \/\/ \u628a\u6293\u53d6\u7684\u4ee3\u7801\u5199\u5165\u8be5\u6587\u4ef6&nbsp;&nbsp; <br \/>$st = fopen($save_to,\"a\");&nbsp;&nbsp; <br \/>&nbsp;&nbsp;<br \/>$mh = curl_multi_init();&nbsp;&nbsp; <br \/>foreach ($urls as $i =&gt; $url) {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;$conn[$i] = curl_init($url);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_USERAGENT, \"Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 6.0)\");&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_HEADER ,0);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,60);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,true);&nbsp;&nbsp;\/\/ \u8bbe\u7f6e\u4e0d\u5c06\u722c\u53d6\u4ee3\u7801\u5199\u5230\u6d4f\u89c8\u5668\uff0c\u800c\u662f\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_add_handle ($mh,$conn[$i]);&nbsp;&nbsp; <br \/>}&nbsp;&nbsp; <br \/>&nbsp;&nbsp;<br \/>do {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_exec($mh,$active);&nbsp;&nbsp; <br \/>} while ($active);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>foreach ($urls as $i =&gt; $url) {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;$data = curl_multi_getcontent($conn[$i]); \/\/ \u83b7\u5f97\u722c\u53d6\u7684\u4ee3\u7801\u5b57\u7b26\u4e32&nbsp;&nbsp; <br \/>&nbsp;&nbsp;fwrite($st,$data);&nbsp;&nbsp;\/\/ \u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5199\u5165\u6587\u4ef6\uff0c\u6bd4\u5982\u5b58\u5165\u6570\u636e\u5e93&nbsp;&nbsp; <br \/>} \/\/ \u83b7\u5f97\u6570\u636e\u53d8\u91cf\uff0c\u5e76\u5199\u5165\u6587\u4ef6&nbsp;&nbsp; <br \/>&nbsp;&nbsp;<br \/>foreach ($urls as $i =&gt; $url) {&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_multi_remove_handle($mh,$conn[$i]);&nbsp;&nbsp; <br \/>&nbsp;&nbsp;curl_close($conn[$i]);&nbsp;&nbsp; <br \/>}&nbsp;&nbsp; <br \/>&nbsp;&nbsp;<br \/>curl_multi_close($mh);&nbsp;&nbsp; <br \/>fclose($st);&nbsp;&nbsp;<br \/>?&gt;<br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u5229\u7528 Curl Functions \u53ef\u4ee5\u5b8c\u6210\u5404\u79cd\u4f20\u9001\u6587\u4ef6\u64cd\u4f5c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}