{"id":556,"date":"2022-12-19T17:30:12","date_gmt":"2022-12-19T09:30:12","guid":{"rendered":"http:\/\/www.cyayaa.com\/?page_id=556"},"modified":"2022-12-19T17:30:12","modified_gmt":"2022-12-19T09:30:12","slug":"php-curl%e6%8a%93%e5%8f%96%e7%bd%91%e4%b8%8a%e5%9b%be%e7%89%87","status":"publish","type":"page","link":"http:\/\/www.cyayaa.com\/index.php\/php-curl%e6%8a%93%e5%8f%96%e7%bd%91%e4%b8%8a%e5%9b%be%e7%89%87\/","title":{"rendered":"PHP CURL\u6293\u53d6\u7f51\u4e0a\u56fe\u7247"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u3000\u3000cURL\u7684\u5e95\u5c42\u662f\u7531\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\u5b9e\u73b0\u7684\uff0c\u7528\u4e8e\u83b7\u53d6\u8fdc\u7a0b\u6587\u4ef6\u6216\u4f20\u8f93\u6587\u4ef6\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u7528\u6765\u6a21\u62dfget\/post\u8868\u5355\u63d0\u4ea4\u3002\u4e5f\u53ef\u4ee5\u7528\u6237\u6587\u4ef6\u4e0a\u4f20\uff0c\u722c\u53d6\u6587\u4ef6\uff0c\u652f\u6301FTP\/FTPS,HTTP\/HTTPS\u7b49\u534f\u8bae\uff0c\u901a\u4fd7\u6765\u8bb2\uff0c\u8fd9\u4e9b\u7c7b\u578b\u7684\u670d\u52a1\u5668\uff0ccURL\u90fd\u53ef\u4ee5\u6293\u53d6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3000\u3000\u4eca\u5929\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u7528cURL\u6293\u53d6\u65e0\u804a\u7f51\u7f51\u4e0a\u7684\u56fe\u7247\u3002\u4e0a\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1 &lt;?php\n 2 $url = 'http:\/\/wuliaoo.com\/tuhua\/';\n 3 $ch = curl_init();\n 4 \n 5 curl_setopt($ch,CURLOPT_URL,$url);\n 6 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); \/\/ \u8bbe\u7f6ecurl_exec()\u83b7\u53d6\u7684\u4fe1\u606f\u4ee5\u5b57\u7b26\u6d41\u5f62\u5f0f\u8fd4\u56de\n 7 curl_setopt($ch,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1); \/\/ \u8bbe\u7f6eCURL\u9009\u9879\u4e3aTLS\n 8 \n 9 $data = curl_exec($ch);\n10 $info = curl_getinfo($ch);\n11 curl_close($ch);\n12 \/\/ var_dump(htmlentities($data)); \u6253\u5370\u83b7\u53d6\u7684\u7f51\u9875\u6e90\u7801\uff0c\u4f46\u662f\u4e0d\u52a0htmlentities\u51fd\u6570\u7684\u8bdd\u4f1a\u5c06\u9875\u9762\u8f93\u51fa\n13 preg_match_all('\/&lt;img&#91;^&gt;]*src=(\\'|\\\")(.*?)\\\\1&#91;^&gt;]*&gt;\/i',$data,$array); \/\/ \u5339\u914d\u6b63\u5219\uff0c\u6240\u6709\u7684img\u6807\u7b7e\n14 \n15 $path = '.\/aaimage\/'; \/\/ \u76ee\u5f55\u63d0\u524d\u5efa\u597d\n16 foreach ($array&#91;2] as $k =&gt; $v) {\n17     if(fopen($array&#91;2]&#91;$k],'r')){\n18         ob_clean(); \/\/ \u6e05\u7a7aPHP\u7f13\u5b58\u533a\u7684\u5185\u5bb9\u3002\u8fd9\u4e00\u6b65\u5176\u5b9e\u4e0d\u786e\u5b9a\u9700\u4e0d\u9700\u8981\uff0c\u4f46\u662f\u4e3a\u4e86\u4fdd\u9669\uff0c\u6211\u8fd8\u662f\u52a0\u4e0a\u4e86\u3002\n19         $ch = curl_init();\n20         curl_setopt($ch, CURLOPT_URL, $v);\n21         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n22         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);\n23         $file = curl_exec($ch);\n24         curl_close($ch);\n25         $filename = pathinfo($v, PATHINFO_BASENAME); \/\/ \u83b7\u53d6\u6587\u4ef6\u540d\u548c\u540e\u7f00\n26         \n27         $resource = fopen($path . $filename, 'a');\n28         fwrite($resource, $file);\n29         fclose($resource);\n30     }\n31 \n32 }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u6bb5\u4ee3\u7801\u5c01\u88c5\u597d\u3002\u800c\u4e14\u8fd8\u53ef\u4ee5\u628a\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u6362\u6210file_get_contents()\u7684\u5f62\u5f0f\u3002\u540c\u6837\u53ef\u4ee5\u83b7\u53d6\u7f51\u9875\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function down_pic($url = null){\n    $data = file_get_contents($url);\n    preg_match_all('\/&lt;img&#91;^&gt;]*src=(\\'|\\\")(.*?)\\\\1&#91;^&gt;]*&gt;\/i',$data,$array);\n\n    $path = '.\/aaimage\/';\n    foreach ($array&#91;2] as $k =&gt; $v) {\n        if(fopen($array&#91;2]&#91;$k],'r')){\n\n            $ch = curl_init();\n            curl_setopt($ch, CURLOPT_URL, $v);\n            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);\n            $file = curl_exec($ch);\n            curl_close($ch);\n            $filename = pathinfo($v, PATHINFO_BASENAME);\n\n            $resource = fopen($path . $filename, 'a');\n            fwrite($resource, $file);\n            fclose($resource);\n        }\n\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8fd9\u662f\u83b7\u53d6\u8be5\u7f51\u7ad95\u9875\u7684\u56fe\u7247\nfor($i=1;$i&lt;=5;$i++){\n    $url = 'http:\/\/wuliaoo.com\/tuhua\/page\/'.$i;\n    down_pic($url);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u83b7\u53d6\u9875\u6570\u5404\u4e2a\u7f51\u7ad9\u4e0d\u662f\u56fa\u5b9a\u3002<br>\u6240\u4ee5\u7528for\u5faa\u73af\u83b7\u53d6\u7684\u8bdd\u76f4\u63a5\u62fc\u4e0a\u540e\u9762\u7684\u53c2\u6570\u5373\u53ef\u3002\u5f53\u7136\uff0c\u4ee3\u7801\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u4f18\u5316\u7684\u5730\u65b9\u3002\u6b22\u8fce\u6307\u6b63\u3002<br>\u4ee5\u4e0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3000cURL\u7684\u5e95\u5c42\u662f\u7531\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\u5b9e\u73b0\u7684\uff0c\u7528\u4e8e\u83b7\u53d6\u8fdc\u7a0b\u6587\u4ef6\u6216\u4f20\u8f93\u6587\u4ef6\uff0c\u66f4\u591a\u7684\u60c5\u51b5\u662f\u7528\u6765\u6a21\u62dfget\/post\u8868 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.cyayaa.com\/index.php\/php-curl%e6%8a%93%e5%8f%96%e7%bd%91%e4%b8%8a%e5%9b%be%e7%89%87\/\"> <span class=\"screen-reader-text\">PHP CURL\u6293\u53d6\u7f51\u4e0a\u56fe\u7247<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"class_list":["post-556","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/pages\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":2,"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/pages\/556\/revisions"}],"predecessor-version":[{"id":558,"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/pages\/556\/revisions\/558"}],"wp:attachment":[{"href":"http:\/\/www.cyayaa.com\/index.php\/wp-json\/wp\/v2\/media?parent=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}