{"id":24006,"date":"2024-11-21T17:10:52","date_gmt":"2024-11-21T09:10:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24006\/"},"modified":"2024-11-21T17:10:52","modified_gmt":"2024-11-21T09:10:52","slug":"swoole%e5%a6%82%e4%bd%95%e6%94%af%e6%8c%81%e5%bc%82%e6%ad%a5http-2%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24006\/","title":{"rendered":"Swoole\u5982\u4f55\u652f\u6301\u5f02\u6b65HTTP\/2\u8fde\u63a5"},"content":{"rendered":"<p>http\/2\u662f\u4e00\u79cd\u65b0\u7684\u534f\u8bae\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5feb\u7684\u901f\u5ea6\u548c\u66f4\u5f3a\u7684\u6027\u80fd\uff0c\u4ece\u800c\u4f7fweb\u66f4\u52a0\u9ad8\u6548\u3002\u5728http\/2\u4e2d\uff0c\u4e00\u6b21\u8fde\u63a5\u53ef\u4ee5\u652f\u6301\u591a\u4e2a\u5e76\u884c\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u8fd9\u5c31\u662f\u201c\u591a\u8def\u590d\u7528\u201d\u3002\u662f\u57fa\u4e8ephp\u7684\u5f02\u6b65\u5e76\u53d1\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u652f\u6301\u5f02\u6b65http\/2\u8fde\u63a5\u3002\u672c\u6587\u5c06\u4ecb\u7ecdswoole\u5982\u4f55\u652f\u6301\u5f02\u6b65http\/2\u8fde\u63a5\u3002<\/p>\n<h3>HTTP\/2\u7684\u5f02\u6b65\u8fde\u63a5<\/h3>\n<p>HTTP\/2\u662f\u4e00\u79cd\u5b8c\u5168\u4e0d\u540c\u4e8eHTTP\/1.1\u7684\u534f\u8bae\uff0c\u5b83\u91c7\u7528\u4e86\u4e8c\u8fdb\u5236\u5206\u5e27\u673a\u5236\uff0c\u5bf9\u8fde\u63a5\u7684\u4f7f\u7528\u4e5f\u6709\u4e86\u5f88\u5927\u7684\u53d8\u5316\u3002\u5728HTTP\/1.1\u4e2d\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u9700\u8981\u5efa\u7acb\u4e00\u4e2aTCP\u8fde\u63a5\u3002\u800c\u5728HTTP\/2\u4e2d\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5728\u4e00\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\uff0c\u8fd9\u5c31\u662f\u591a\u8def\u590d\u7528\u673a\u5236\u3002HTTP\/2\u7684\u591a\u8def\u590d\u7528\u673a\u5236\u4f7f\u5f97\u5ba2\u6237\u7aef\u53ef\u4ee5\u540c\u65f6\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\u5e76\u4e14\u4e0d\u4f1a\u6709\u5ef6\u8fdf\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u7f51\u7edc\u5ef6\u8fdf\uff0c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002<\/p>\n<h3>Swoole\u7684\u5f02\u6b65HTTP\/2\u8fde\u63a5<\/h3>\n<p>Swoole\u662f\u4e00\u79cdPHP\u7684\u5f02\u6b65\u5e76\u53d1\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u652f\u6301\u5f02\u6b65HTTP\/2\u8fde\u63a5\u3002Swoole\u7684\u5f02\u6b65HTTP\/2\u8fde\u63a5\u4f7f\u7528\u4e86\u5e95\u5c42\u7684nghttp2\u5e93\u6765\u5b9e\u73b0\u3002\u5bf9\u4e8eHTTP\/2\u534f\u8bae\u6765\u8bf4\uff0cSwoole\u63d0\u4f9b\u4e86HTTP\/2\u5ba2\u6237\u7aef\u548cHTTP\/2\u670d\u52a1\u5668\u7684\u652f\u6301\u3002<\/p>\n<h4>Swoole\u7684\u5f02\u6b65HTTP\/2\u670d\u52a1\u5668<\/h4>\n<p>Swoole\u63d0\u4f9b\u4e86HTTP\/2\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u8fde\u63a5\u4e0a\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\uff1a<\/p>\n<pre>$http = new SwooleHttpServer(\"127.0.0.1\", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);\n$http-&amp;gt;set([\n    'ssl_cert_file' =&amp;gt; '\/path\/to\/cert.crt',\n    'ssl_key_file' =&amp;gt; '\/path\/to\/key.key',\n]);\n$http-&amp;gt;on('Request', function (SwooleHttpRequest $request, SwooleHttpResponse $response) {\n    $response-&amp;gt;header('Content-Type', 'text\/plain');\n    $response-&amp;gt;end(\"Hello World\n\");\n});\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u521b\u5efa\u4e86\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\u5e76\u76d1\u542c\u5728\u672c\u5730\u76849501\u7aef\u53e3\u3002\u5728HTTP\/2\u670d\u52a1\u5668\u7684\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0c\u6211\u4eec\u7b80\u5355\u5730\u8fd4\u56de\u4e86\u4e00\u4e2a\u201cHello World\u201d\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2aHTTP\/2\u670d\u52a1\u5668\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2aTCP\u8fde\u63a5\u6765\u5904\u7406\u6240\u6709\u7684HTTP\/2\u8bf7\u6c42\u3002<\/p>\n<h4>Swoole\u7684\u5f02\u6b65HTTP\/2\u5ba2\u6237\u7aef<\/h4>\n<p>Swoole\u7684HTTP\/2\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u7684\u65b9\u5f0f\u5411HTTP\/2\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u4e14\u53ef\u4ee5\u5f02\u6b65\u5730\u63a5\u6536HTTP\/2\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65HTTP\/2\u5ba2\u6237\u7aef\uff1a<\/p>\n<pre>$http2 = new SwooleHttp2Client('www.example.com', 443, true);\n$http2-&amp;gt;set([\n    'ssl_cert_file' =&amp;gt; '\/path\/to\/cert.crt',\n    'ssl_key_file' =&amp;gt; '\/path\/to\/key.key',\n]);\n$http2-&amp;gt;connect(function() use ($http2) {\n    $http2-&amp;gt;send(1, [\n        [\":method\" =&amp;gt; \"GET\", \":path\" =&amp;gt; \"\/\"],\n    ], '');\n});\n\n$http2-&amp;gt;recv(1, function($http2, $streamId, $data) {\n    echo $data;\n    $http2-&amp;gt;close();\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u521b\u5efa\u4e86\u4e00\u4e2aHTTP\/2\u5ba2\u6237\u7aef\uff0c\u5e76\u4e14\u5f02\u6b65\u5730\u65b9\u5f0f\u5411HTTP\/2\u670d\u52a1\u5668\u53d1\u9001\u4e86\u4e00\u6b21GET\u8bf7\u6c42\u3002\u5728\u63a5\u6536\u5230HTTP\/2\u670d\u52a1\u5668\u54cd\u5e94\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u5f02\u6b65\u5730\u5904\u7406HTTP\/2\u5ba2\u6237\u7aef\u7684\u54cd\u5e94\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528Swoole\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u6765\u5b9e\u73b0\u9ad8\u6548\u7684HTTP\/2\u5ba2\u6237\u7aef\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>Swoole\u662f\u57fa\u4e8ePHP\u7684\u5f02\u6b65\u5e76\u53d1\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u652f\u6301\u5f02\u6b65HTTP\/2\u8fde\u63a5\u3002\u5728HTTP\/2\u4e2d\uff0c\u4e00\u6b21\u8fde\u63a5\u53ef\u4ee5\u652f\u6301\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u8fd9\u4f7f\u5f97Web\u66f4\u52a0\u9ad8\u6548\u548c\u5feb\u901f\u3002\u901a\u8fc7\u4f7f\u7528Swoole\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528HTTP\/2\u534f\u8bae\u6765\u52a0\u901f\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u800c\u63d0\u9ad8\u6211\u4eec\u7684Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5982\u4f55\u652f\u6301\u5f02\u6b65HTTP\/2\u8fde\u63a5\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>http\/2\u662f\u4e00\u79cd\u65b0\u7684\u534f\u8bae\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5feb\u7684\u901f\u5ea6\u548c\u66f4\u5f3a\u7684\u6027\u80fd\uff0c\u4ece\u800c\u4f7fweb\u66f4\u52a0\u9ad8\u6548\u3002\u5728http\/2\u4e2d\uff0c\u4e00\u6b21\u8fde\u63a5\u53ef\u4ee5\u652f\u6301\u591a\u4e2a\u5e76\u884c\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u8fd9\u5c31\u662f\u201c\u591a\u8def\u590d\u7528\u201d\u3002\u662f\u57fa\u4e8ephp\u7684\u5f02\u6b65\u5e76\u53d1\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u652f\u6301\u5f02\u6b65http\/2\u8fde\u63a5\u3002\u672c\u6587\u5c06\u4ecb\u7ecdswoole\u5982\u4f55\u652f\u6301\u5f02\u6b65http\/2\u8fde\u63a5\u3002 HTTP\/2\u7684\u5f02\u6b65\u8fde\u63a5 HTTP\/2\u662f\u4e00\u79cd\u5b8c\u5168\u4e0d\u540c\u4e8eHTTP\/1.1\u7684\u534f\u8bae\uff0c\u5b83\u91c7\u7528\u4e86\u4e8c\u8fdb\u5236\u5206\u5e27\u673a\u5236\uff0c\u5bf9\u8fde\u63a5\u7684\u4f7f\u7528\u4e5f\u6709\u4e86\u5f88\u5927\u7684\u53d8\u5316\u3002\u5728HTTP\/1.1\u4e2d\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u9700\u8981\u5efa\u7acb\u4e00\u4e2aTCP\u8fde\u63a5\u3002\u800c\u5728HTTP\/2\u4e2d\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5728\u4e00\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\uff0c\u8fd9\u5c31\u662f\u591a\u8def\u590d\u7528\u673a\u5236\u3002HTTP\/2\u7684\u591a\u8def\u590d\u7528\u673a\u5236\u4f7f\u5f97\u5ba2\u6237\u7aef\u53ef\u4ee5\u540c\u65f6\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\u5e76\u4e14\u4e0d\u4f1a\u6709\u5ef6\u8fdf\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u7f51\u7edc\u5ef6\u8fdf\uff0c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002 Swoole\u7684\u5f02\u6b65HTTP\/2\u8fde\u63a5 Swoole\u662f\u4e00\u79cdPHP\u7684\u5f02\u6b65\u5e76\u53d1\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u652f\u6301\u5f02\u6b65HTTP\/2\u8fde\u63a5\u3002Swoole\u7684\u5f02\u6b65HTTP\/2\u8fde\u63a5\u4f7f\u7528\u4e86\u5e95\u5c42\u7684nghttp2\u5e93\u6765\u5b9e\u73b0\u3002\u5bf9\u4e8eHTTP\/2\u534f\u8bae\u6765\u8bf4\uff0cSwoole\u63d0\u4f9b\u4e86HTTP\/2\u5ba2\u6237\u7aef\u548cHTTP\/2\u670d\u52a1\u5668\u7684\u652f\u6301\u3002 Swoole\u7684\u5f02\u6b65HTTP\/2\u670d\u52a1\u5668 Swoole\u63d0\u4f9b\u4e86HTTP\/2\u670d\u52a1\u5668\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u8fde\u63a5\u4e0a\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\uff1a $http = new SwooleHttpServer(&#8220;127.0.0.1&#8221;, 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL); $http-&amp;gt;set([ &#8216;ssl_cert_file&#8217; =&amp;gt; &#8216;\/path\/to\/cert.crt&#8217;, &#8216;ssl_key_file&#8217; =&amp;gt; &#8216;\/path\/to\/key.key&#8217;, ]); $http-&amp;gt;on(&#8216;Request&#8217;, function (SwooleHttpRequest $request, SwooleHttpResponse $response) { $response-&amp;gt;header(&#8216;Content-Type&#8217;, &#8216;text\/plain&#8217;); $response-&amp;gt;end(&#8220;Hello World &#8220;); }); $http-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u521b\u5efa\u4e86\u4e00\u4e2aHTTP\/2\u670d\u52a1\u5668\u5e76\u76d1\u542c\u5728\u672c\u5730\u76849501\u7aef\u53e3\u3002\u5728HTTP\/2\u670d\u52a1\u5668\u7684\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0c\u6211\u4eec\u7b80\u5355\u5730\u8fd4\u56de\u4e86\u4e00\u4e2a\u201cHello World\u201d\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2aHTTP\/2\u670d\u52a1\u5668\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2aTCP\u8fde\u63a5\u6765\u5904\u7406\u6240\u6709\u7684HTTP\/2\u8bf7\u6c42\u3002 Swoole\u7684\u5f02\u6b65HTTP\/2\u5ba2\u6237\u7aef Swoole\u7684HTTP\/2\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u7684\u65b9\u5f0f\u5411HTTP\/2\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u4e14\u53ef\u4ee5\u5f02\u6b65\u5730\u63a5\u6536HTTP\/2\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65HTTP\/2\u5ba2\u6237\u7aef\uff1a $http2 = new SwooleHttp2Client(&#8216;www.example.com&#8217;, 443, true); $http2-&amp;gt;set([ &#8216;ssl_cert_file&#8217; =&amp;gt; &#8216;\/path\/to\/cert.crt&#8217;, &#8216;ssl_key_file&#8217; =&amp;gt; [&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-24006","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24006","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=24006"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24006\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}