{"id":20,"date":"2021-07-22T18:38:02","date_gmt":"2021-07-22T10:38:02","guid":{"rendered":"http:\/\/wp.cn\/?p=20"},"modified":"2022-04-15T16:11:39","modified_gmt":"2022-04-15T08:11:39","slug":"php%e7%9a%84curl%e8%af%b7%e6%b1%82%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=20","title":{"rendered":"PHP\u7684curl\u8bf7\u6c42\u65b9\u6cd5"},"content":{"rendered":"<pre class=\"language-php\"><code>&lt;?php\r\nnamespace app\\index\\controller;\r\n \r\nclass Temp\r\n{\r\n    \/\/\u6a21\u62df\u8bf7\u6c42\u6e90\uff0c\u53c2\u65701\uff1a\u8bbf\u95ee\u7684URL\uff0c\u53c2\u65702\uff1apost\u6570\u636e(\u4e0d\u586b\u5219\u4e3aGET)\uff0c\u53c2\u65703\uff1a\u63d0\u4ea4\u7684$cookies,\u53c2\u65704\uff1a\u662f\u5426\u8fd4\u56de$cookies\r\n    public function new_curl_request($url,$post='',$cookie='', $returnCookie=0){\r\n        \/\/\u6a21\u62df\u8bf7\u6c42\u6e90IP\u5730\u5740\u3010\u6682\u7528\u767e\u5ea6\u5730\u5740\u6bb5\u3011\r\n        $cip = '123.125.68.'.mt_rand(0,254);\r\n        $xip = '125.90.88.'.mt_rand(0,254);\r\n        $header = array(\r\n            'CLIENT-IP:'.$cip,\r\n            'X-FORWARDED-FOR:'.$xip,\r\n        );\r\n \r\n        $curl = curl_init();\r\n        curl_setopt($curl, CURLOPT_URL, $url);\r\n        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla\/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident\/6.0)');\r\n        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);\r\n        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);\r\n        curl_setopt ($curl, CURLOPT_HTTPHEADER, $header);\r\n        curl_setopt($curl, CURLOPT_REFERER, \"http:\/\/www.baidu.com\/\");\r\n        if($post) {\r\n            curl_setopt($curl, CURLOPT_POST, 1);\r\n            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));\r\n        }\r\n        if($cookie) {\r\n            curl_setopt($curl, CURLOPT_COOKIE, $cookie);\r\n        }\r\n        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);\r\n        curl_setopt($curl, CURLOPT_TIMEOUT, 10);\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\r\n        $data = curl_exec($curl);\r\n        if (curl_errno($curl)) {\r\n            return curl_error($curl);\r\n        }\r\n        curl_close($curl);\r\n        if($returnCookie){\r\n            list($header, $body) = explode(\"\\r\\n\\r\\n\", $data, 2);\r\n            preg_match_all(\"\/Set\\-Cookie:([^;]*);\/\", $header, $matches);\r\n            $info['cookie']  = substr($matches[1][0], 1);\r\n            $info['content'] = $body;\r\n            return $info;\r\n        }else{\r\n            return $data;\r\n        }\r\n    }\r\n \r\n    \/\/\u65e0\u6a21\u62df\u8bf7\u6c42\u6e90\uff0c\u53c2\u65701\uff1a\u8bbf\u95ee\u7684URL\uff0c\u53c2\u65702\uff1apost\u6570\u636e(\u4e0d\u586b\u5219\u4e3aGET)\uff0c\u53c2\u65703\uff1a\u63d0\u4ea4\u7684$cookies,\u53c2\u65704\uff1a\u662f\u5426\u8fd4\u56de$cookies\r\n    public function curl_request($url,$post='',$cookie='', $returnCookie=0){\r\n        $curl = curl_init();\r\n        curl_setopt($curl, CURLOPT_URL, $url);\r\n        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla\/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident\/6.0)');\r\n        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);\r\n        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);\r\n        curl_setopt($curl, CURLOPT_REFERER, \"http:\/\/XXX\");\r\n        if($post) {\r\n            curl_setopt($curl, CURLOPT_POST, 1);\r\n            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));\r\n        }\r\n        if($cookie) {\r\n            curl_setopt($curl, CURLOPT_COOKIE, $cookie);\r\n        }\r\n        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);\r\n        curl_setopt($curl, CURLOPT_TIMEOUT, 10);\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\r\n        $data = curl_exec($curl);\r\n        if (curl_errno($curl)) {\r\n            return curl_error($curl);\r\n        }\r\n        curl_close($curl);\r\n        if($returnCookie){\r\n            list($header, $body) = explode(\"\\r\\n\\r\\n\", $data, 2);\r\n            preg_match_all(\"\/Set\\-Cookie:([^;]*);\/\", $header, $matches);\r\n            $info['cookie']  = substr($matches[1][0], 1);\r\n            $info['content'] = $body;\r\n            return $info;\r\n        }else{\r\n            return $data;\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * CURL\u8bf7\u6c42\u3010\u8bf7\u6c42https\u65f6\u7684\u8bc1\u4e66\u5904\u7406\u3011\r\n     * @param string $url \u8bbf\u95ee\u7684URL\r\n     * @param string $post post\u6570\u636e(\u4e0d\u586b\u5219\u4e3aGET)\r\n     * @param string $header header\u5934\u6570\u7ec4\r\n     * @param string $cookie \u63d0\u4ea4\u7684$cookies\r\n     * @param int $returnCookie \u662f\u5426\u8fd4\u56de$cookies\r\n     * @return array|bool|string\r\n     *\/\r\n    private function curl_request($url, $post = [], $header = [], $cookie = '', $returnCookie = 0) {\r\n        $curl = curl_init();\r\n        curl_setopt($curl, CURLOPT_URL, $url);\r\n        \/\/\u6709\u9700\u8981https\u7684\u73af\u5883\u3010\u9700\u8981\u914d\u7f6eSSL\u8bc1\u4e66\u4f4d\u7f6e\u3011\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); \/\/ \u5bf9\u8ba4\u8bc1\u8bc1\u4e66\u6765\u6e90\u7684\u68c0\u67e5\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); \/\/ \u4ece\u8bc1\u4e66\u4e2d\u68c0\u67e5SSL\u52a0\u5bc6\u7b97\u6cd5\u662f\u5426\u5b58\u5728\r\n        curl_setopt($curl, CURLOPT_SSLVERSION, 0);\/\/\u8bbe\u7f6eSSL\u534f\u8bae\u7248\u672c\u53f7\r\n        \/\/curl_setopt($curl, CURLOPT_CAINFO,'\/etc\/ssl\/cacert.pem');  \/\/\u914d\u7f6e\u8bc1\u4e66\u4f4d\u7f6e \u6700\u65b0\u8bc1\u4e66\u4e0b\u8f7d\uff1ahttps:\/\/curl.se\/docs\/caextract.html\r\n\r\n        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla\/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident\/6.0)');\r\n        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);\r\n        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);\r\n        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);\r\n        curl_setopt($curl, CURLOPT_REFERER, \"\");\r\n        if (!empty($post)) {\r\n            curl_setopt($curl, CURLOPT_POST, 1);\r\n            \/\/curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));\r\n            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);\r\n        }\r\n        if ($cookie) {\r\n            curl_setopt($curl, CURLOPT_COOKIE, $cookie);\r\n        }\r\n        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);\r\n        curl_setopt($curl, CURLOPT_TIMEOUT, 10);\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\r\n        $data = curl_exec($curl);\r\n        if (curl_errno($curl)) {\r\n            return curl_error($curl);\r\n        }\r\n        curl_close($curl);\r\n        if ($returnCookie) {\r\n            list($header, $body) = explode(\"\\r\\n\\r\\n\", $data, 2);\r\n            preg_match_all(\"\/Set\\-Cookie:([^;]*);\/\", $header, $matches);\r\n            $info['cookie'] = substr($matches[1][0], 1);\r\n            $info['content'] = $body;\r\n            return $info;\r\n        } else {\r\n            return $data;\r\n        }\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php namespace app\\index\\controller; class Temp { \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[61],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-php","tag-curl"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}