{"id":3133,"date":"2026-02-21T12:47:42","date_gmt":"2026-02-21T04:47:42","guid":{"rendered":"https:\/\/blog.nonot.cn\/?p=3133"},"modified":"2026-02-21T12:47:42","modified_gmt":"2026-02-21T04:47:42","slug":"php%e4%bd%bf%e7%94%a8curl%e5%87%bd%e6%95%b0%e5%8f%91%e9%80%81post%e8%af%b7%e6%b1%82%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2026\/02\/21\/php%e4%bd%bf%e7%94%a8curl%e5%87%bd%e6%95%b0%e5%8f%91%e9%80%81post%e8%af%b7%e6%b1%82%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9\/","title":{"rendered":"PHP\u4f7f\u7528curl\u51fd\u6570\u53d1\u9001Post\u8bf7\u6c42\u7684\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<p>\u5f53\u7528\u6237\u70b9\u51fb\u4e86\u67d0\u4e2a\u6309\u94ae\u4ee5\u540e\uff0c\u540e\u53f0\u53d1\u8d77\u4e00\u4e2apost\u8bf7\u6c42\u5230\u6211\u8fd9\u91cc\uff0c\u6211\u63a5\u53d7\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002\u4e00\u5f00\u59cb\u540c\u4e8b\u662f\u7528\u516c\u53f8\u6846\u67b6\u516c\u5171\u5c01\u88c5\u7684<code>http_request()<\/code>\u65b9\u6cd5\u8fdb\u884c\u53d1\u9001\u7684\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>public function http_request( $url, $post = &#8221;, $timeout = 5 ){<br \/>\nif( empty( $url ) ){<br \/>\nreturn ;<br \/>\n}<br \/>\n$ch = curl_init();<br \/>\ncurl_setopt($ch, CURLOPT_URL, $url);<br \/>\ncurl_setopt($ch, CURLOPT_HEADER, 0);<br \/>\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br \/>\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br \/>\ncurl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<\/p>\n<p>if( $post != &#8221; &amp;&amp; !empty( $post ) ){<br \/>\ncurl_setopt($ch, CURLOPT_POST, 1);<br \/>\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $post);<br \/>\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array(&#8216;Content-Type: application\/json&#8217;, &#8216;Content-Length: &#8216; . strlen($post)));<br \/>\n}<br \/>\ncurl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br \/>\n$result = curl_exec($ch);<br \/>\ncurl_close($ch);<br \/>\nreturn $result;<br \/>\n}<\/p>\n<p>\u4e00\u5f00\u59cb\u6211\u5e76\u6ca1\u6709\u7559\u610f\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u662f<code>application\/json<\/code>\u7f16\u7801\u7684json\u5b57\u7b26\u4e32\uff0c\u6211\u5728\u540e\u53f0\u76f4\u63a5\u7528\u63a5\u53d7<code>application\/x-www-form-urlencoded<\/code>\u7f16\u7801\u683c\u5f0f\u7684\u6570\u636e\u65b9\u5f0f\u6765\u53d6\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e(\u5c31\u662f\u76f4\u63a5\u7528\u7684<code>$_POST<\/code>\u65b9\u5f0f\u83b7\u53d6\u7684)\uff0c\u7ed3\u679c\u5f53\u7136\u6ca1\u4ec0\u4e48\u4e5f\u6ca1\u6709\u53d6\u5230\u4e86\u3002\u540e\u6765\uff0c\u540c\u4e8b\u76f4\u63a5\u6539\u4e86<code>http_request()<\/code>\u65b9\u6cd5\uff0c\u76f4\u63a5\u4f20\u9012<code>application\/x-www-form-urlencoded<\/code>\u7f16\u7801\u683c\u5f0f\u7684\u6570\u636e\u8fc7\u6765\uff0c\u6211\u8fd9\u5c31\u6ca1\u6709\u505a\u66f4\u6539\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u6211\u4e00\u76f4\u7eb3\u95f7\u5f53\u65f6\u4e3a\u4ec0\u4e48\u6ca1\u6709\u62ff\u5230\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u4eca\u5929\u9879\u76ee\u57fa\u672c\u5b8c\u5de5\uff0c\u7814\u7a76\u4e86\u4ee5\u4e0b\u3002<\/strong><\/p>\n<p>php\u4e2d\u7684<code>curl()<\/code>\u51fd\u6570\u8fdb\u884cpost\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u4f20\u9012\u6570\u636e\u7684\u683c\u5f0f\u53ef\u4ee5\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f:<\/p>\n<p>(1):\u7531\u53c2\u6570\u62fc\u63a5\u800c\u6210\u7684<code>key=&gt;value<\/code>\u952e\u503c\u5bf9\u5b57\u7b26\u4e32\u3002\u5f62\u5982\u4ee5\u4e0b\uff1a\u00a0<code>name=xxx&amp;age=23$sex=1<\/code><\/p>\n<p>\u8fd9\u79cd\u8bf7\u6c42\u53c2\u6570\u9ed8\u8ba4\u662f\u6309\u7167<code>application\/x-www-form-urlencoded<\/code>\u8fdb\u884c\u7f16\u7801\u7684\u3002<\/p>\n<p>(2):\u7531\u53c2\u6570\u7ec4\u6210\u7684<code>key=&gt;value<\/code>\u952e\u503c\u5bf9\u6570\u7ec4(\u53ea\u80fd\u662f\u4e00\u7ef4\u6570\u7ec4,\u66f4\u9ad8\u7ef4\u5ea6\u7684\u6570\u7ec4\u4f1a\u62a5\u9519)\u3002<\/p>\n<p>\u5f62\u5982\u4ee5\u4e0b\u683c\u5f0f:<\/p>\n<div class=\"jb51code\">\n<div>\n<div id=\"highlighter_430140\" class=\"syntaxhighlighter  php\">[ name=&#8221;xxx&#8221; , age = 23 , sex = \u7537 ]<\/div>\n<div>\n<p>\u8fd9\u79cd\u8bf7\u6c42\u53c2\u6570\u9ed8\u8ba4\u662f\u6309\u7167<code>multipart\/form-data<\/code>\u683c\u5f0f\u8fdb\u884c\u7f16\u7801\u7684\u3002<\/p>\n<p>\u4e0a\u9762\u8bf4\u4e86,<code>curl()<\/code>\u8fdb\u884cpost\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u4f20\u9012\u4e00\u7ef4\u6570\u7ec4\u4f5c\u4e3a\u4f20\u9012\u7684\u53c2\u6570\uff0c\u90a3\u4e48\u5982\u679c\u60f3\u8981\u4f20\u9012\u591a\u7ef4\u6570\u7ec4\u9700\u8981\u600e\u4e48\u5904\u7406\u90a3\uff1f<\/p>\n<p><strong>\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6765\u5904\u7406\uff0c\u5206\u522b\u662f\u4e0b\u9762\u7684\u65b9\u5f0f3\u4ee5\u53ca\u65b9\u5f0f4\u3002<\/strong><\/p>\n<p>(3):\u5c06\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c<code>http_build_query()<\/code>\u8fdb\u884c\u5904\u7406\uff0c\u7b49\u5230\u4e00\u4e2akey=&gt;value\u952e\u503c\u5bf9\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5982\u4e0b\u9762\u6240\u793a:<\/p>\n<p>$data = [<br \/>\n&#8220;msg&#8221;=&gt;&#8221;\u8fd9\u662f\u4e00\u6761\u6d4b\u8bd5\u6570\u636e&#8221;,<br \/>\n&#8220;xxx&#8221; =&gt; &#8220;yyyy&#8221;,<br \/>\n&#8220;msg_data&#8221; =&gt; [<br \/>\n&#8220;name&#8221;=&gt;&#8221;sunms&#8221;,<br \/>\n&#8220;age&#8221;=&gt;23,<br \/>\n&#8220;sex&#8221;=&gt;&#8221;\u7537&#8221;,<br \/>\n&#8220;content&#8221;=&gt;[<br \/>\n1,2,3<br \/>\n]<br \/>\n],<br \/>\n];<\/p>\n<p>\u5c06\u5f97\u5230\u4ee5\u4e0b\u7684\u5b57\u7b26\u4e32:<\/p>\n<p>msg=\u8fd9\u662f\u4e00\u6761\u6d4b\u8bd5\u6570\u636e&amp;xxx=yyyy&amp;msg_data[name]=sunms&amp;msg_data[age]=23&amp;msg_data[sex]=\u7537&amp;msg_data[content][0]=1&amp;msg_data[content][1]=2&amp;msg_data[content][2]=3<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u662f\u901a\u8fc7<code>application\/x-www-form-urlencoded\u8fdb<\/code>\u884c\u7f16\u7801\u7684\uff0c\u5728\u63a5\u6536\u65b9\u53ef\u4ee5\u901a\u8fc7$_POST\u76f4\u63a5\u83b7\u53d6\u3002<\/p>\n<p>(4):\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c<code>application\/json<\/code>\u683c\u5f0f\u7f16\u7801\uff0c\u5728\u63a5\u6536\u65b9\u901a\u8fc7<code>file_get_contents(\u201cphp:\/\/input\u201d)<\/code>\u6216\u8005<code>$GLOBALS[\u2018HTTP_RAW_POST_DATA']<\/code>\u7684\u65b9\u5f0f\u83b7\u53d6\u4f20\u9012\u8fc7\u6765\u7684json\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06json\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>$data = [];<br \/>\n$data_string = json_encode($data);<br \/>\n&#8230;..<br \/>\n\/\/\u8bbe\u7f6eheader\u4fe1\u606f<br \/>\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array(<br \/>\n&#8216;Content-Type: application\/json&#8217;,<br \/>\n&#8216;Content-Length: &#8216; . strlen($data_string))<br \/>\n);<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u5bf9\u4e8e<code>application\/json<\/code>\u683c\u5f0f\u7f16\u7801\u7684\u6570\u636e\uff0c<code>$_POST<\/code>\u662f\u4e0d\u80fd\u76f4\u63a5\u83b7\u53d6\u7684\uff0c\u9700\u8981\u901a\u8fc7<code>file_get_contents(\u201cphp:\/\/input\u201d)<\/code>\u6216\u8005<code>$GLOBALS[\u2018HTTP_RAW_POST_DATA']<\/code>\u7684\u65b9\u5f0f\u83b7\u53d6\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u7528\u6237\u70b9\u51fb\u4e86\u67d0\u4e2a\u6309\u94ae\u4ee5\u540e\uff0c\u540e\u53f0\u53d1\u8d77\u4e00\u4e2apost\u8bf7\u6c42\u5230\u6211\u8fd9\u91cc\uff0c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3133","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3133"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3133\/revisions"}],"predecessor-version":[{"id":3134,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3133\/revisions\/3134"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}