{"id":1075,"date":"2019-04-06T15:25:39","date_gmt":"2019-04-06T07:25:39","guid":{"rendered":"https:\/\/freesilo.com\/?p=1075"},"modified":"2019-04-06T15:25:39","modified_gmt":"2019-04-06T07:25:39","slug":"php%e8%ae%bf%e9%97%aeurl%ef%bc%88get%e5%92%8cpost%e8%af%b7%e6%b1%82%ef%bc%89","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=1075","title":{"rendered":"php\u8bbf\u95eeurl\uff08get\u548cpost\u8bf7\u6c42\uff09"},"content":{"rendered":"<p><strong>get\u8bf7\u6c42<\/strong><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n    \/*\r\n     *  php\u8bbf\u95eeurl\u8def\u5f84\uff0cget\u8bf7\u6c42\r\n     *\/\r\n    function curl_file_get_contents($durl){\r\n        \/\/ header\u4f20\u9001\u683c\u5f0f\r\n        $headers = array(\r\n            &quot;token:1111111111111&quot;,\r\n            &quot;over_time:22222222222&quot;,\r\n        );\r\n        \/\/ \u521d\u59cb\u5316\r\n        $curl = curl_init();\r\n        \/\/ \u8bbe\u7f6eurl\u8def\u5f84\r\n        curl_setopt($curl, CURLOPT_URL, $durl);\r\n        \/\/ \u5c06 curl_exec()\u83b7\u53d6\u7684\u4fe1\u606f\u4ee5\u6587\u4ef6\u6d41\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8f93\u51fa\u3002\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;\r\n        \/\/ \u5728\u542f\u7528 CURLOPT_RETURNTRANSFER \u65f6\u5019\u5c06\u83b7\u53d6\u6570\u636e\u8fd4\u56de\r\n        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;\r\n        \/\/ \u6dfb\u52a0\u5934\u4fe1\u606f\r\n        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);\r\n        \/\/ CURLINFO_HEADER_OUT\u9009\u9879\u53ef\u4ee5\u62ff\u5230\u8bf7\u6c42\u5934\u4fe1\u606f\r\n        curl_setopt($curl, CURLINFO_HEADER_OUT, true);\r\n        \/\/ \u4e0d\u9a8c\u8bc1SSL\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);\r\n        \/\/ \u6267\u884c\r\n        $data = curl_exec($curl);\r\n        \/\/ \u6253\u5370\u8bf7\u6c42\u5934\u4fe1\u606f\r\n        \/\/ echo curl_getinfo($curl, CURLINFO_HEADER_OUT);\r\n        \/\/ \u5173\u95ed\u8fde\u63a5\r\n        curl_close($curl);\r\n        \/\/ \u8fd4\u56de\u6570\u636e\r\n        return $data;\r\n    }\r\n<\/pre>\n<p><strong>post\u8bf7\u6c42<\/strong><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/*\r\n     *   php\u8bbf\u95eeurl\u8def\u5f84\uff0cpost\u8bf7\u6c42\r\n     *\r\n     *   durl   \u8def\u5f84url\r\n     *   post_data   array()   post\u53c2\u6570\u6570\u636e\r\n     *\/\r\n    public function curl_file_post_contents($durl, $post_data){\r\n        \/\/ header\u4f20\u9001\u683c\u5f0f\r\n        $headers = array(\r\n            &quot;token:1111111111111&quot;,\r\n            &quot;over_time:22222222222&quot;,\r\n        );\r\n        \/\/\u521d\u59cb\u5316\r\n        $curl = curl_init();\r\n        \/\/\u8bbe\u7f6e\u6293\u53d6\u7684url\r\n        curl_setopt($curl, CURLOPT_URL, $durl);\r\n        \/\/\u8bbe\u7f6e\u5934\u6587\u4ef6\u7684\u4fe1\u606f\u4f5c\u4e3a\u6570\u636e\u6d41\u8f93\u51fa\r\n        curl_setopt($curl, CURLOPT_HEADER, false);\r\n        \/\/\u8bbe\u7f6e\u83b7\u53d6\u7684\u4fe1\u606f\u4ee5\u6587\u4ef6\u6d41\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8f93\u51fa\u3002\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\r\n        \/\/\u8bbe\u7f6epost\u65b9\u5f0f\u63d0\u4ea4\r\n        curl_setopt($curl, CURLOPT_POST, true);\r\n        \/\/ \u8bbe\u7f6epost\u8bf7\u6c42\u53c2\u6570\r\n        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);\r\n        \/\/ CURLINFO_HEADER_OUT\u9009\u9879\u53ef\u4ee5\u62ff\u5230\u8bf7\u6c42\u5934\u4fe1\u606f\r\n        curl_setopt($curl, CURLINFO_HEADER_OUT, true);\r\n        \/\/ \u4e0d\u9a8c\u8bc1SSL\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);\r\n        \/\/\u6267\u884c\u547d\u4ee4\r\n        $data = curl_exec($curl);\r\n        \/\/ \u6253\u5370\u8bf7\u6c42\u5934\u4fe1\u606f\r\n        \/\/ echo curl_getinfo($curl, CURLINFO_HEADER_OUT);\r\n        \/\/\u5173\u95edURL\u8bf7\u6c42\r\n        curl_close($curl);\r\n        \/\/\u663e\u793a\u83b7\u5f97\u7684\u6570\u636e\r\n        return $data;\r\n    }\r\n<\/pre>\n<p>\u6ce8\uff1a\u8c03\u7528\u540e\u53ef\u4ee5\u901a\u8fc7json_decode\u6765\u89e3\u6790\u8fd4\u56de\u7ed3\u679c\uff1a\u5982\uff1a<br \/>\n$data= $this-&gt;curl_file_post_contents($dataurl, $post_data);<br \/>\n$dataarr = json_decode($data, true);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>get\u8bf7\u6c42 \/* * php\u8bbf\u95eeurl\u8def\u5f84\uff0cget\u8bf7\u6c42 *\/ function &#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=1075\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1075"}],"version-history":[{"count":1,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1075\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1075\/revisions\/1076"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}