{"id":3135,"date":"2026-02-21T12:51:15","date_gmt":"2026-02-21T04:51:15","guid":{"rendered":"https:\/\/blog.nonot.cn\/?p=3135"},"modified":"2026-02-21T12:51:15","modified_gmt":"2026-02-21T04:51:15","slug":"php%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8curl%e5%8f%91%e9%80%81%e8%af%b7%e6%b1%82%ef%bc%88get%e8%af%b7%e6%b1%82%e5%92%8cpost%e8%af%b7%e6%b1%82%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2026\/02\/21\/php%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8curl%e5%8f%91%e9%80%81%e8%af%b7%e6%b1%82%ef%bc%88get%e8%af%b7%e6%b1%82%e5%92%8cpost%e8%af%b7%e6%b1%82%ef%bc%89\/","title":{"rendered":"PHP\u4e2d\u7684\u4f7f\u7528curl\u53d1\u9001\u8bf7\u6c42\uff08GET\u8bf7\u6c42\u548cPOST\u8bf7\u6c42\uff09"},"content":{"rendered":"<p><strong>\u4f7f\u7528CURL\u53d1\u9001\u8bf7\u6c42\u7684\u57fa\u672c\u6d41\u7a0b<\/strong><\/p>\n<p>\u4f7f\u7528CURL\u7684PHP\u6269\u5c55\u5b8c\u6210\u4e00\u4e2aHTTP\u8bf7\u6c42\u7684\u53d1\u9001\u4e00\u822c\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p>1.\u521d\u59cb\u5316\u8fde\u63a5\u53e5\u67c4\uff1b<\/p>\n<p>2.\u8bbe\u7f6eCURL\u9009\u9879\uff1b<\/p>\n<p>3.\u6267\u884c\u5e76\u83b7\u53d6\u7ed3\u679c\uff1b<\/p>\n<p>4.\u91ca\u653eVURL\u8fde\u63a5\u53e5\u67c4\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\u7247\u6bb5\u662f\u4f7f\u7528CURL\u53d1\u9001HTTP\u7684\u5178\u578b\u8fc7\u7a0b<\/p>\n<p>\/\/ 1. \u521d\u59cb\u5316<br \/>\n$ch = curl_init();<br \/>\n\/\/ 2. \u8bbe\u7f6e\u9009\u9879\uff0c\u5305\u62ecURL<br \/>\ncurl_setopt($ch,CURLOPT_URL,&#8221;http:\/\/www.devdo.net&#8221;);<br \/>\ncurl_setopt($ch,CURLOPT_RETURNTRANSFER,1);<br \/>\ncurl_setopt($ch,CURLOPT_HEADER,0);<br \/>\n\/\/ 3. \u6267\u884c\u5e76\u83b7\u53d6HTML\u6587\u6863\u5185\u5bb9<br \/>\n$output = curl_exec($ch);<br \/>\nif($output === FALSE ){<br \/>\necho &#8220;CURL Error:&#8221;.curl_error($ch);<br \/>\n}<br \/>\n\/\/ 4. \u91ca\u653ecurl\u53e5\u67c4<br \/>\ncurl_close($ch);<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u4f7f\u7528\u5230\u4e86\u56db\u4e2a\u51fd\u6570<\/p>\n<ul>\n<li>curl_init() \u548c curl_close() \u5206\u522b\u662f\u521d\u59cb\u5316CURL\u8fde\u63a5\u548c\u5173\u95edCURL\u8fde\u63a5\uff0c\u90fd\u6bd4\u8f83\u7b80\u5355\u3002<\/li>\n<li>curl_exec() \u6267\u884cCURL\u8bf7\u6c42\uff0c\u5982\u679c\u6ca1\u6709\u9519\u8bef\u53d1\u751f\uff0c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u662f\u5bf9\u5e94URL\u8fd4\u56de\u7684\u6570\u636e\uff0c\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u6ee1\u610f\uff1b\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u8be5\u51fd\u6570\u8fd4\u56de FALSE\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5224\u65ad\u8f93\u51fa\u662f\u5426\u4e3aFALSE\u7528\u7684\u662f\u5168\u7b49\u53f7\uff0c\u8fd9\u662f\u4e3a\u4e86\u533a\u5206\u8fd4\u56de\u7a7a\u4e32\u548c\u51fa\u9519\u7684\u60c5\u51b5\u3002<\/li>\n<li>CURL\u51fd\u6570\u5e93\u91cc\u6700\u91cd\u8981\u7684\u51fd\u6570\u662fcurl_setopt(),\u5b83\u53ef\u4ee5\u901a\u8fc7\u8bbe\u5b9aCURL\u51fd\u6570\u5e93\u5b9a\u4e49\u7684\u9009\u9879\u6765\u5b9a\u5236HTTP\u8bf7\u6c42\u3002\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\u4f7f\u7528\u4e86\u4e09\u4e2a\u91cd\u8981\u7684\u9009\u9879\uff1a<\/li>\n<\/ul>\n<p>\u2460CURLOPT_URL \u6307\u5b9a\u8bf7\u6c42\u7684URL\uff1b<\/p>\n<p>\u2461CURLOPT_RETURNTRANSFER \u8bbe\u7f6e\u4e3a1\u8868\u793a\u7a0d\u540e\u6267\u884c\u7684curl_exec\u51fd\u6570\u7684\u8fd4\u56de\u662fURL\u7684\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u628a\u8fd4\u56de\u5b57\u7b26\u4e32\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u5e76\u8fd4\u56deTRUE\uff1b<\/p>\n<p>\u2462CURLLOPT_HEADER\u8bbe\u7f6e\u4e3a0\u8868\u793a\u4e0d\u8fd4\u56deHTTP\u5934\u90e8\u4fe1\u606f\u3002<\/p>\n<p>CURL\u7684\u9009\u9879\u8fd8\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u5230PHP\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"http:\/\/www.php.net\/manual\/en\/function.curl-setopt.php\" rel=\"external nofollow\">http:\/\/www.php.net\/manual\/en\/function.curl-setopt.php<\/a>\uff09\u4e0a\u67e5\u770bCURL\u652f\u6301\u7684\u6240\u6709\u9009\u9879\u5217\u8868\u3002<\/p>\n<p><strong>\u83b7\u53d6CURL\u8bf7\u6c42\u7684\u8f93\u51fa\u4fe1\u606f<\/strong><\/p>\n<p>\u5728curl_exec()\u51fd\u6570\u6267\u884c\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528curl_getinfo()\u51fd\u6570\u83b7\u53d6CURL\u8bf7\u6c42\u8f93\u51fa\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>curl_exec($ch);<br \/>\n$info = curl_getinfo($sh);<br \/>\necho &#8216; \u83b7\u53d6 &#8216;.$info[&#8216;url&#8217;].&#8217;\u8017\u65f6&#8217;.$info[&#8216;total_time&#8217;].&#8217;\u79d2&#8217;;<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2dcurl_getinfo\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<ul>\n<li>url:\u7f51\u7edc\u5730\u5740\u3002<\/li>\n<li>content_type:\u5185\u5bb9\u7f16\u7801\u3002<\/li>\n<li>http_code:HTTP\u72b6\u6001\u7801\u3002<\/li>\n<li>header_size:header\u7684\u5927\u5c0f\u3002<\/li>\n<li>request_size:\u8bf7\u6c42\u7684\u5927\u5c0f\u3002<\/li>\n<li>filetime:\u6587\u4ef6\u521b\u5efa\u7684\u65f6\u95f4\u3002<\/li>\n<li>ssl_verify_result:SSL\u9a8c\u8bc1\u7ed3\u679c\u3002<\/li>\n<li>redirect_count:\u8df3\u8f6c\u8ba1\u6570\u3002<\/li>\n<li>total_time:\u603b\u8017\u65f6\u3002<\/li>\n<li>namelookup_time:DNS\u67e5\u8be2\u8017\u65f6\u3002<\/li>\n<li>connect_time:\u7b49\u5f85\u8fde\u63a5\u8017\u65f6\u3002<\/li>\n<li>pretransfer_time:\u4f20\u8f93\u524d\u51c6\u5907\u8017\u65f6\u3002<\/li>\n<li>size_uplpad:\u4e0a\u4f20\u6570\u636e\u7684\u5927\u5c0f\u3002<\/li>\n<li>size_download:\u4e0b\u8f7d\u6570\u636e\u7684\u5927\u5c0f\u3002<\/li>\n<li>speed_download:\u4e0b\u8f7d\u901f\u5ea6\u3002<\/li>\n<li>speed_upload:\u4e0a\u4f20\u901f\u5ea6\u3002<\/li>\n<li>download_content_length:\u4e0b\u8f7d\u5185\u5bb9\u7684\u957f\u5ea6\u3002<\/li>\n<li>upload_content_length:\u4e0a\u4f20\u5185\u5bb9\u7684\u957f\u5ea6\u3002<\/li>\n<li>starttransfer_time:\u5f00\u59cb\u4f20\u8f93\u7684\u65f6\u95f4\u8868\u3002<\/li>\n<li>redirect_time:\u91cd\u5b9a\u5411\u8017\u65f6\u3002<\/li>\n<\/ul>\n<p>curl_getinfo()\u51fd\u6570\u8fd8\u6709\u4e00\u4e2a\u53ef\u9009\u62e9\u53c2\u6570$opt,\u901a\u8fc7\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u5e38\u91cf\uff0c\u5bf9\u5e94\u5230\u4e0a\u672f\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u53ea\u6709\u6307\u5b9a\u7684\u4fe1\u606f\u3002\u4f8b\u5982\u8bbe\u7f6e$opt\u4e3aCURLINFO_TOTAL_TIME\uff0c\u5219curl_getinfo()\u51fd\u6570\u53ea\u8fd4\u56detotal_time,\u5373\u603b\u4f20\u8f93\u6d88\u8017\u7684\u65f6\u95f4\uff0c\u5728\u53ea\u9700\u8981\u5173\u6ce8\u67d0\u4e9b\u4f20\u8f93\u4fe1\u606f\u65f6\uff0c\u8bbe\u7f6e$opt\u53c2\u6570\u5f88\u6709\u610f\u4e49\u3002<\/p>\n<p><strong>\u4f7f\u7528CURL\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528CURL\u6765\u53d1\u9001GET\u8bf7\u6c42\uff0c\u53d1\u9001GET\u8bf7\u6c42\u7684\u5173\u952e\u662f\u62fc\u88c5\u683c\u5f0f\u6b63\u786e\u7684URL\u3002\u8bf7\u6c42\u5730\u5740\u548cGET\u6570\u636e\u7531\u4e00\u4e2a\u201c?\u201d\u5206\u5272,\u7136\u540eGET\u53d8\u91cf\u7684\u540d\u79f0\u548c\u503c\u7528\u201c=\u201d\u5206\u9694\uff0c\u5404\u4e2aGET\u540d\u79f0\u548c\u503c\u7531\u201c&amp;\u201d\u8fde\u63a5\u3002PHP\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570\u4e13\u95e8\u7528\u6765\u62fc\u88c5GET\u8bf7\u6c42\u548c\u6570\u636e\u90e8\u5206\u2014\u2014http_build_query,\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u8fd4\u56de\u7531\u8be5\u5173\u8054\u6570\u636e\u63cf\u8ff0\u7684GET\u8bf7\u6c42\u5b57\u7b26\u4e32\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u7ed3\u5408CURL\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u4e00\u822c\u6d41\u7a0b\uff0c\u6211\u4eec\u5c01\u95ed\u4e86\u4e00\u4e2a\u53d1\u9001GET\u8bf7\u6c42\u7684\u51fd\u6570\u2014\u2014doCurlGetRequest,\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\/**<br \/>\n*@desc \u5c01\u95edcurl\u7684\u8c03\u7528\u63a5\u53e3\uff0cget\u7684\u8bf7\u6c42\u65b9\u5f0f\u3002<br \/>\n*\/<br \/>\nfunction doCurlGetRequest($url,$data,$timeout = 5){<br \/>\nif($curl == &#8220;&#8221; || $timeout &lt;= 0){<br \/>\nreturn false;<br \/>\n}<br \/>\n$url = $url.&#8217;?&#8217;.http_bulid_query($data);<br \/>\n$con = curl_init((string)$url);<br \/>\ncurl_setopt($con, CURLOPT_HEADER, false);<br \/>\ncurl_setopt($con, CURLOPT_RETURNTRANSFER,true);<br \/>\ncurl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);<\/p>\n<p>return curl_exec($con);<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u628a\u4f7f\u7528http_build_query \u62fc\u88c5\u597d\u7684\u5e26GET\u53c2\u6570\u7684URL\u4f20\u7ed9curl_init\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528CURL\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528CURL\u53d1\u9001POST\u8bf7\u6c42<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528CURL\u63d0\u4f9b\u7684\u9009\u9879CURLOPT_POSTFIELDS\uff0c\u8bbe\u7f6e\u8be5\u9009\u9879\u4e3aPOST\u5b57\u7b26\u4e32\u6570\u636e\u5c31\u53ef\u4ee5\u628a\u8bf7\u6c42\u653e\u5728\u6b63\u6587\u4e2d\u3002\u540c\u6837\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53d1\u9001POST\u8bf7\u6c42\u7684\u51fd\u6570\u2014\u2014doCurlPostRequest\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\/**<br \/>\n** @desc \u5c01\u88c5 curl \u7684\u8c03\u7528\u63a5\u53e3\uff0cpost\u7684\u8bf7\u6c42\u65b9\u5f0f<br \/>\n**\/<br \/>\nfunction doCurlPostRequest($url,$requestString,$timeout = 5){<br \/>\nif($url == &#8221; || $requestString == &#8221; || $timeout &lt;=0){<br \/>\nreturn false;<br \/>\n}<br \/>\n$con = curl_init((string)$url);<br \/>\ncurl_setopt($con, CURLOPT_HEADER, false);<br \/>\ncurl_setopt($con, CURLOPT_POSTFIELDS, $requestString);<br \/>\ncurl_setopt($con, CURLOPT_POST,true);<br \/>\ncurl_setopt($con, CURLOPT_RETURNTRANSFER,true);<br \/>\ncurl_setopt($con, CURLOPT_TIMEOUT,(int)$timeout);<br \/>\nreturn curl_exec($con);<br \/>\n}<\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\u9664\u4e86\u8bbe\u7f6eCURLOPT_POSTFIELDS\u5916\uff0c\u6211\u4eec\u8fd8\u8bbe\u7f6e\u4e86CURL_POST\u4e3atrue,\u6807\u8bc6\u8fd9\u4e2a\u8bf7\u6c42\u662f\u4e00\u4e2aPOST\u8bf7\u6c42\u3002\u5728POST\u8bf7\u6c42\u4e2d\u4e5f\u662f\u53ef\u4ee5\u4f20\u8f93GET\u6570\u636e\u7684\uff0c\u53ea\u9700\u8981\u5728URL\u4e2d\u62fc\u88c5GET\u8bf7\u6c42\u6570\u636e\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528CURL\u53d1\u9001\u8bf7\u6c42\u7684\u57fa\u672c\u6d41\u7a0b \u4f7f\u7528CURL\u7684PHP\u6269\u5c55\u5b8c\u6210&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-3135","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3135","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=3135"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3135\/revisions"}],"predecessor-version":[{"id":3136,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/3135\/revisions\/3136"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}