{"id":23500,"date":"2024-11-21T12:52:21","date_gmt":"2024-11-21T04:52:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23500\/"},"modified":"2024-11-21T12:52:21","modified_gmt":"2024-11-21T04:52:21","slug":"swoole%e5%ae%9e%e7%8e%b0%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e5%bc%82%e6%ad%a5http%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23500\/","title":{"rendered":"Swoole\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u5f02\u6b65HTTP\u5ba2\u6237\u7aef"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u65f6\u4ee3\u7684\u4e0d\u65ad\u53d1\u5c55\uff0chttp\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u7f51\u7edc\u534f\u8bae\u4e4b\u4e00\uff0c\u5176\u91cd\u8981\u6027\u4e5f\u8d8a\u6765\u8d8a\u51f8\u663e\u3002\u5728\u65e5\u5e38\u7684 web \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4e0d\u65ad\u83b7\u53d6\u5916\u90e8\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u5e38\u89c4\u7684 http \u5ba2\u6237\u7aef\u5f80\u5f80\u9700\u8981\u4e0d\u65ad\u5730\u53d1\u8d77\u8bf7\u6c42\uff0c\u7b49\u5f85\u670d\u52a1\u5668\u8fd4\u56de\u6570\u636e\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5f80\u5f80\u6548\u7387\u4f4e\u4e0b\uff0c\u6781\u6613\u9020\u6210\u8d44\u6e90\u6d6a\u8d39\u3002<\/p>\n<p>\u4e3a\u6b64\uff0cSwoole \u5e73\u53f0\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e TCP \u534f\u8bae\u7684\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\uff0c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684HTTP\u6570\u636e\u83b7\u53d6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u539f\u7406\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u6f14\u793a\u5176\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u4e00\u3001Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u539f\u7406<\/p>\n<p>Swoole \u5e73\u53f0\u4f7f\u7528\u4e86\u5e95\u5c42\u7684 epoll \u548c Linux \u5185\u6838\u5f02\u6b65 IO \u6280\u672f\uff0c\u53ef\u4ee5\u6781\u5927\u63d0\u5347\u7f51\u7edc IO \u6548\u7387\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\uff0c\u5176\u4e2d Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u4e5f\u662f\u57fa\u4e8e\u6b64\u6280\u672f\u5b9e\u73b0\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528 Swoole \u7684 SwooleCoroutineHttpClient \u7c7b\uff0c\u5373\u53ef\u5b9e\u73b0\u5f02\u6b65\u7684 HTTP \u6570\u636e\u8bf7\u6c42\u3002\u8be5\u7c7b\u7ee7\u627f\u81ea Swoole \u5e73\u53f0\u63d0\u4f9b\u7684\u534f\u7a0b\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528\u534f\u7a0b\u6280\u672f\u5b9e\u73b0\u5f02\u6b65\u8bf7\u6c42\u3002<\/p>\n<p>\u5bf9\u4e8e\u666e\u901a\u540c\u6b65\u7684 HTTP \u8bf7\u6c42\uff0c\u9700\u8981\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\uff0c\u8bf7\u6c42\u6570\u636e\uff0c\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\uff0c\u518d\u8fd4\u56de\u7ed3\u679c\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u5f80\u5f80\u88ab\u963b\u585e\uff0c\u65e0\u6cd5\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u9020\u6210\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<p>\u5728 Swoole \u5e73\u53f0\u4e0a\u4f7f\u7528\u5f02\u6b65\u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u5728\u53d1\u9001\u8bf7\u6c42\u540e\u7acb\u5373\u8fd4\u56de\uff0c\u7136\u540e\u901a\u8fc7\u534f\u7a0b\u6280\u672f\uff0c\u8ba9\u7ebf\u7a0b\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7684\u8bf7\u6c42\u3002\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5b8c\u6210\u540e\uff0c\u5f02\u6b65\u5ba2\u6237\u7aef\u4f1a\u81ea\u52a8\u5c06\u54cd\u5e94\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<p>\u4e8c\u3001\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u4f7f\u7528<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\uff0c\u5b9e\u73b0\u5f02\u6b65 HTTP \u8bf7\u6c42\uff1a<\/p>\n<pre>$client = new SwooleCoroutineHttpClient('www.baidu.com', 443, true);\n$client-&amp;gt;setHeaders([\n    'Host' =&amp;gt; 'www.baidu.com',\n    'User-Agent' =&amp;gt; 'Chrome\/49.0.2587.3',\n    'Accept' =&amp;gt; 'text\/html,application\/xhtml+xml,application\/xml',\n    'Accept-Encoding' =&amp;gt; 'gzip',\n]);\n$client-&amp;gt;set(['timeout' =&amp;gt; 1]);\n$client-&amp;gt;get('\/');\n$response = $client-&amp;gt;body;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\uff0c\u7136\u540e\u8bbe\u7f6e\u4e86\u8bf7\u6c42\u5934\u90e8\u4fe1\u606f\u3001\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\u7b49\u53c2\u6570\uff0c\u6700\u540e\u901a\u8fc7 $client-&gt;get(&#8216;\/&#8217;) \u53d1\u9001\u4e86\u4e00\u4e2a GET \u8bf7\u6c42\uff0c\u5e76\u5c06\u54cd\u5e94\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf $response\u3002<\/p>\n<p>\u5728\u53d1\u9001\u8bf7\u6c42\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7684\u8bf7\u6c42\uff0c\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5b8c\u6210\u540e\uff0cSwoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u4f1a\u901a\u8fc7\u534f\u7a0b\u6280\u672f\u5c06\u54cd\u5e94\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u54cd\u5e94\u7ed3\u679c\u88ab\u4fdd\u5b58\u5728\u53d8\u91cf $response \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\u3001\u5904\u7406\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u5728\u5f53\u4eca\u7684\u9ad8\u5e76\u53d1\u7f51\u7edc\u73af\u5883\u4e0b\uff0c\u4f7f\u7528\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u7f51\u7edc\u8bf7\u6c42\u6548\u7387\uff0c\u4ece\u800c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u66f4\u5feb\u6377\u3001\u9ad8\u6548\u7684\u7f51\u7edc\u652f\u6301\u3002\u901a\u8fc7 Swoole \u5e73\u53f0\u63d0\u4f9b\u7684\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65\u7684 HTTP \u6570\u636e\u83b7\u53d6\uff0c\u5e26\u6765\u6781\u4f73\u7684\u7f51\u7edc\u8bf7\u6c42\u6548\u7387\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9 PHP \u4e2d\u5e38\u89c1\u7684 cURL \u5e93\u5b9e\u73b0 HTTP \u8bf7\u6c42\uff0c\u4e5f\u53ef\u4ee5\u5229\u7528 Swoole \u5e73\u53f0\u63d0\u4f9b\u7684\u5f02\u6b65\u6280\u672f\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u5f02\u6b65HTTP\u5ba2\u6237\u7aef\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u65f6\u4ee3\u7684\u4e0d\u65ad\u53d1\u5c55\uff0chttp\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u7f51\u7edc\u534f\u8bae\u4e4b\u4e00\uff0c\u5176\u91cd\u8981\u6027\u4e5f\u8d8a\u6765\u8d8a\u51f8\u663e\u3002\u5728\u65e5\u5e38\u7684 web \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4e0d\u65ad\u83b7\u53d6\u5916\u90e8\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u5e38\u89c4\u7684 http \u5ba2\u6237\u7aef\u5f80\u5f80\u9700\u8981\u4e0d\u65ad\u5730\u53d1\u8d77\u8bf7\u6c42\uff0c\u7b49\u5f85\u670d\u52a1\u5668\u8fd4\u56de\u6570\u636e\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5f80\u5f80\u6548\u7387\u4f4e\u4e0b\uff0c\u6781\u6613\u9020\u6210\u8d44\u6e90\u6d6a\u8d39\u3002 \u4e3a\u6b64\uff0cSwoole \u5e73\u53f0\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e TCP \u534f\u8bae\u7684\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\uff0c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684HTTP\u6570\u636e\u83b7\u53d6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u5b9e\u73b0\u539f\u7406\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u6f14\u793a\u5176\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u4f7f\u7528\u3002 \u4e00\u3001Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u539f\u7406 Swoole \u5e73\u53f0\u4f7f\u7528\u4e86\u5e95\u5c42\u7684 epoll \u548c Linux \u5185\u6838\u5f02\u6b65 IO \u6280\u672f\uff0c\u53ef\u4ee5\u6781\u5927\u63d0\u5347\u7f51\u7edc IO \u6548\u7387\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\uff0c\u5176\u4e2d Swoole \u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u4e5f\u662f\u57fa\u4e8e\u6b64\u6280\u672f\u5b9e\u73b0\u3002 \u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528 Swoole \u7684 SwooleCoroutineHttpClient \u7c7b\uff0c\u5373\u53ef\u5b9e\u73b0\u5f02\u6b65\u7684 HTTP \u6570\u636e\u8bf7\u6c42\u3002\u8be5\u7c7b\u7ee7\u627f\u81ea Swoole \u5e73\u53f0\u63d0\u4f9b\u7684\u534f\u7a0b\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528\u534f\u7a0b\u6280\u672f\u5b9e\u73b0\u5f02\u6b65\u8bf7\u6c42\u3002 \u5bf9\u4e8e\u666e\u901a\u540c\u6b65\u7684 HTTP \u8bf7\u6c42\uff0c\u9700\u8981\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\uff0c\u8bf7\u6c42\u6570\u636e\uff0c\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\uff0c\u518d\u8fd4\u56de\u7ed3\u679c\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u5f80\u5f80\u88ab\u963b\u585e\uff0c\u65e0\u6cd5\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u9020\u6210\u6548\u7387\u4f4e\u4e0b\u3002 \u5728 Swoole \u5e73\u53f0\u4e0a\u4f7f\u7528\u5f02\u6b65\u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u5728\u53d1\u9001\u8bf7\u6c42\u540e\u7acb\u5373\u8fd4\u56de\uff0c\u7136\u540e\u901a\u8fc7\u534f\u7a0b\u6280\u672f\uff0c\u8ba9\u7ebf\u7a0b\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7684\u8bf7\u6c42\u3002\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5b8c\u6210\u540e\uff0c\u5f02\u6b65\u5ba2\u6237\u7aef\u4f1a\u81ea\u52a8\u5c06\u54cd\u5e94\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u7f51\u7edc\u8bf7\u6c42\u3002 \u4e8c\u3001\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u7684\u4f7f\u7528 \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\uff0c\u5b9e\u73b0\u5f02\u6b65 HTTP [&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-23500","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23500","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=23500"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23500\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}