{"id":48270,"date":"2024-12-02T13:51:55","date_gmt":"2024-12-02T05:51:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48270\/"},"modified":"2024-12-02T13:51:55","modified_gmt":"2024-12-02T05:51:55","slug":"php%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9ahttp%e5%8d%8f%e8%ae%ae%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48270\/","title":{"rendered":"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730006789671dcf05675b4.jpg\" class=\"aligncenter\" title=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8\u63d2\u56fe\" alt=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP \u534f\u8bae\u5165\u95e8<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>HTTP\uff08\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae\uff09\u662f\u4e00\u79cd\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u534f\u8bae\uff0c\u7528\u4e8e\u5728\u4e07\u7ef4\u7f51\u4e0a\u4f20\u8f93\u6570\u636e\u3002\u5b83\u662f Web \u7684\u57fa\u7840\uff0c\u8ba9\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u7ad9\u3002\u672c\u6307\u5357\u5c06\u4ecb\u7ecd HTTP \u534f\u8bae\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<p><strong>HTTP \u6d88\u606f\u683c\u5f0f<\/strong><\/p>\n<p>HTTP \u6d88\u606f\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<ul>\n<li><strong>\u8bf7\u6c42<\/strong>\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6d88\u606f\uff0c\u5305\u542b\u5bf9\u8d44\u6e90\u7684\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u54cd\u5e94<\/strong>\uff1a\u670d\u52a1\u5668\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u6d88\u606f\uff0c\u5305\u542b\u8bf7\u6c42\u7684\u8d44\u6e90\u6216\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u4e00\u4e2a\u5178\u578b\u7684 HTTP \u8bf7\u6c42\u6d88\u606f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>GET \/index.html HTTP\/1.1\nHost: www.example.com<\/pre>\n<p>\u6b64\u8bf7\u6c42\u6d88\u606f\u8bf7\u6c42\u83b7\u53d6\u8d44\u6e90 <code>\/index.html<\/code>\u3002\u670d\u52a1\u5668\u5c06\u4f7f\u7528\u4ee5\u4e0b\u54cd\u5e94\u6d88\u606f\u505a\u51fa\u54cd\u5e94\uff1a<\/p>\n<pre>HTTP\/1.1 200 OK\nContent-Type: text\/html\nContent-Length: 123\n\n&lt;html&gt;...&lt;\/html&gt;<\/pre>\n<p>\u6b64\u54cd\u5e94\u6d88\u606f\u8868\u793a\u8bf7\u6c42\u5df2\u6210\u529f\uff0c\u5e76\u4e14\u670d\u52a1\u5668\u5df2\u53d1\u9001\u4e00\u4e2a\u957f\u5ea6\u4e3a 123 \u5b57\u8282\u7684 HTML \u6587\u6863\u3002<\/p>\n<p><strong>HTTP \u65b9\u6cd5<\/strong><\/p>\n<p>HTTP \u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u7528\u4e8e\u6307\u5b9a\u5ba2\u6237\u7aef\u5bf9\u8d44\u6e90\u6267\u884c\u7684\u64cd\u4f5c\u3002\u6700\u5e38\u89c1\u7684 HTTP \u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>GET<\/strong>\uff1a\u83b7\u53d6\u8d44\u6e90<\/li>\n<li><strong>POST<\/strong>\uff1a\u521b\u5efa\u6216\u66f4\u65b0\u8d44\u6e90<\/li>\n<li><strong>PUT<\/strong>\uff1a\u66ff\u6362\u73b0\u6709\u8d44\u6e90<\/li>\n<li><strong>DELETE<\/strong>\uff1a\u5220\u9664\u8d44\u6e90<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528 HTTP \u534f\u8bae\u3002\u6211\u4eec\u5c06\u4f7f\u7528 cURL \u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u5728 PHP \u4e2d\u8fdb\u884c HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u6709\u7528\u5e93\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5 cURL \u5e93\uff1a<\/p>\n<pre>composer require php-curl\/curl<\/pre>\n<p>\u7136\u540e\uff0c\u521b\u5efa\u4ee5\u4e0b PHP \u811a\u672c\uff08<code>http-request.php<\/code>\uff09\uff1a<\/p>\n<pre>&lt;?php\n\n$url = 'http:\/\/www.example.com\/index.html';\n\n$ch = curl_init();\ncurl_setopt($ch, CURLOPT_URL, $url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n\n$response = curl_exec($ch);\ncurl_close($ch);\n\necho $response;<\/pre>\n<p>\u6b64\u811a\u672c\u4f7f\u7528 cURL \u53d1\u51fa GET \u8bf7\u6c42\u4ee5\u83b7\u53d6 <code>$url<\/code> \u6307\u5b9a\u7684\u8d44\u6e90\u3002\u54cd\u5e94\u5b58\u50a8\u5728 <code>$response<\/code> \u53d8\u91cf\u4e2d\uff0c\u5e76\u4f7f\u7528 <code>echo<\/code> \u8f93\u51fa\u3002<\/p>\n<p><strong>\u8fd0\u884c\u811a\u672c<\/strong><\/p>\n<p>\u901a\u8fc7\u7ec8\u7aef\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<pre>php http-request.php<\/pre>\n<p>\u6b64\u547d\u4ee4\u5c06\u6253\u5370 <code>index.html<\/code> \u6587\u6863\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,HTTP\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8 \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP\u534f\u8bae\u5165\u95e8\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aHTTP \u534f\u8bae\u5165\u95e8 \u7b80\u4ecb HTTP\uff08\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae\uff09\u662f\u4e00\u79cd\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u534f\u8bae\uff0c\u7528\u4e8e\u5728\u4e07\u7ef4\u7f51\u4e0a\u4f20\u8f93\u6570\u636e\u3002\u5b83\u662f Web \u7684\u57fa\u7840\uff0c\u8ba9\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u7ad9\u3002\u672c\u6307\u5357\u5c06\u4ecb\u7ecd HTTP \u534f\u8bae\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u5b83\u3002 HTTP \u6d88\u606f\u683c\u5f0f HTTP \u6d88\u606f\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a \u8bf7\u6c42\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6d88\u606f\uff0c\u5305\u542b\u5bf9\u8d44\u6e90\u7684\u8bf7\u6c42\u3002 \u54cd\u5e94\uff1a\u670d\u52a1\u5668\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u6d88\u606f\uff0c\u5305\u542b\u8bf7\u6c42\u7684\u8d44\u6e90\u6216\u9519\u8bef\u4fe1\u606f\u3002 \u4e00\u4e2a\u5178\u578b\u7684 HTTP \u8bf7\u6c42\u6d88\u606f\u5982\u4e0b\u6240\u793a\uff1a GET \/index.html HTTP\/1.1 Host: www.example.com \u6b64\u8bf7\u6c42\u6d88\u606f\u8bf7\u6c42\u83b7\u53d6\u8d44\u6e90 \/index.html\u3002\u670d\u52a1\u5668\u5c06\u4f7f\u7528\u4ee5\u4e0b\u54cd\u5e94\u6d88\u606f\u505a\u51fa\u54cd\u5e94\uff1a HTTP\/1.1 200 OK Content-Type: text\/html Content-Length: 123 &lt;html&gt;&#8230;&lt;\/html&gt; \u6b64\u54cd\u5e94\u6d88\u606f\u8868\u793a\u8bf7\u6c42\u5df2\u6210\u529f\uff0c\u5e76\u4e14\u670d\u52a1\u5668\u5df2\u53d1\u9001\u4e00\u4e2a\u957f\u5ea6\u4e3a 123 \u5b57\u8282\u7684 HTML \u6587\u6863\u3002 HTTP \u65b9\u6cd5 HTTP \u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u7528\u4e8e\u6307\u5b9a\u5ba2\u6237\u7aef\u5bf9\u8d44\u6e90\u6267\u884c\u7684\u64cd\u4f5c\u3002\u6700\u5e38\u89c1\u7684 HTTP \u65b9\u6cd5\u5305\u62ec\uff1a GET\uff1a\u83b7\u53d6\u8d44\u6e90 POST\uff1a\u521b\u5efa\u6216\u66f4\u65b0\u8d44\u6e90 PUT\uff1a\u66ff\u6362\u73b0\u6709\u8d44\u6e90 DELETE\uff1a\u5220\u9664\u8d44\u6e90 \u5b9e\u6218\u6848\u4f8b \u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u5728 PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48270","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48270"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48270\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}