{"id":46855,"date":"2024-12-02T09:33:03","date_gmt":"2024-12-02T01:33:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46855\/"},"modified":"2024-12-02T09:33:03","modified_gmt":"2024-12-02T01:33:03","slug":"php-%e5%87%bd%e6%95%b0%e6%89%a9%e5%b1%95%e4%b8%8e-swoole-%e6%89%a9%e5%b1%95%e6%9c%89%e4%bb%80%e4%b9%88%e5%85%b3%e7%b3%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46855\/","title":{"rendered":"PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u96c6\u6210\uff0c\u5229\u7528\u534f\u7a0b\u673a\u5236\u5b9e\u73b0\u9ad8\u6027\u80fd PHP \u5e94\u7528\u7a0b\u5e8f\u3002\u51fd\u6570\u6269\u5c55\u63d0\u4f9b\u81ea\u5b9a\u4e49\u529f\u80fd\uff0cSwoole \u6269\u5c55\u63d0\u4f9b\u5f02\u6b65\u5e76\u53d1 I\/O\uff0c\u901a\u8fc7\u96c6\u6210\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728 Swoole \u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\uff0c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4f7f\u7528\u51fd\u6570\u6269\u5c55\u81ea\u5b9a\u4e49 Swoole \u8def\u7531\u89c4\u5219\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u903b\u8f91\u5904\u7406\u8bf7\u6c42\uff0c\u5b9e\u73b0\u7075\u6d3b\u6269\u5c55\u4e0e\u9ad8\u6548\u5e76\u53d1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729850367671b6bff20797.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\uff1a\u534f\u7a0b\u5e26\u6765\u7684\u65b0\u4e16\u754c<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u51fd\u6570\u6269\u5c55\u662f PHP \u63d0\u4f9b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u81ea\u5df1\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u5e76\u5c06\u5176\u6269\u5c55\u5230 PHP \u4e2d\u3002Swoole \u6269\u5c55\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5e76\u53d1 Web \u670d\u52a1\u5668\u548c\u5f02\u6b65\u7f16\u7a0b\u6846\u67b6\uff0c\u5229\u7528\u534f\u7a0b\u673a\u5236\u5b9e\u73b0\u9ad8\u6027\u80fd\u7f51\u7edc\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u4eec\u521b\u5efa\u9ad8\u6027\u80fd\u7684 PHP Web \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u534f\u7a0b\u673a\u5236<\/strong><\/p>\n<p>\u4e86\u89e3\u534f\u7a0b\u662f\u7406\u89e3\u51fd\u6570\u6269\u5c55\u548c Swoole \u6269\u5c55\u5173\u7cfb\u7684\u5173\u952e\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5141\u8bb8\u4ee3\u7801\u5728\u4e0d\u5207\u6362\u5806\u6808\u7684\u60c5\u51b5\u4e0b\u5728\u4e0d\u540c\u7684\u4efb\u52a1\u4e4b\u95f4\u5207\u6362\u3002\u8fd9\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u800c\u65e0\u9700\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>Swoole \u6269\u5c55\u5229\u7528\u534f\u7a0b\u673a\u5236\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5e76\u53d1 I\/O \u64cd\u4f5c\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u7f16\u5199\u975e\u963b\u585e\u7684\u4ee3\u7801\uff0c\u6700\u5927\u9650\u5ea6\u5730\u5229\u7528 CPU \u8d44\u6e90\u3002<\/p>\n<p><strong>\u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u7684\u96c6\u6210<\/strong><\/p>\n<p>\u51fd\u6570\u6269\u5c55\u53ef\u4ee5\u4e0e Swoole \u6269\u5c55\u96c6\u6210\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728 Swoole \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u3002\u8fd9\u4e3a\u521b\u5efa\u81ea\u5b9a\u4e49\u529f\u80fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u4f8b\u5982\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3001\u6d88\u606f\u5904\u7406\u7a0b\u5e8f\u6216\u6570\u636e\u8f6c\u6362\u51fd\u6570\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528\u51fd\u6570\u6269\u5c55\u81ea\u5b9a\u4e49 Swoole \u8def\u7531<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u6269\u5c55\u81ea\u5b9a\u4e49 Swoole \u8def\u7531\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u521b\u5efa\u81ea\u5b9a\u4e49\u8def\u7531\u7c7b\nclass MyRouter extends \\Swoole\\Http\\Router\n{\n    public function match(string $path = null): ?\\Swoole\\Http\\Request\n    {\n        \/\/ \u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\n        if ($path === '\/my-custom-route') {\n            \/\/ \u521b\u5efa\u8bf7\u6c42\u5bf9\u8c61\n            $request = new Swoole\\Http\\Request();\n            \/\/ \u8bbe\u7f6e\u8bf7\u6c42\u5c5e\u6027\n            $request-&gt;server['request_uri'] = '\/my-custom-route';\n            \/\/ \u8fd4\u56de\u8bf7\u6c42\u5bf9\u8c61\n            return $request;\n        }\n\n        \/\/ \u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u8fdb\u884c\u9ed8\u8ba4\u8def\u7531\u5339\u914d\n        return parent::match($path);\n    }\n}\n\n\/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u8def\u7531\u7c7b\n$httpServer = new Swoole\\Http\\Server('0.0.0.0', 9501);\n$httpServer-&gt;set([\n    'router' =&gt; new MyRouter(),\n]);\n\n$httpServer-&gt;on('request', function (\\Swoole\\Http\\Request $request, \\Swoole\\Http\\Response $response) {\n    \/\/ \u5904\u7406\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u8bf7\u6c42\n    if ($request-&gt;server['request_uri'] === '\/my-custom-route') {\n        \/\/ \u8fd4\u56de\u54cd\u5e94\n        $response-&gt;status(200);\n        $response-&gt;header('Content-Type', 'text\/html');\n        $response-&gt;end('\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u54cd\u5e94');\n    }\n});\n\n$httpServer-&gt;start();<\/pre>\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c<code>MyRouter<\/code> \u7c7b\u521b\u5efa\u4e3a\u4e00\u4e2a\u51fd\u6570\u6269\u5c55\uff0c\u5b83\u6269\u5c55\u4e86 <code>Swoole\\Http\\Router<\/code> \u7c7b\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\uff0c\u5e76\u4f7f\u7528\u81ea\u5b9a\u4e49\u903b\u8f91\u5904\u7406\u8fd9\u4e9b\u8def\u7531\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u7684\u96c6\u6210\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u9ad8\u6027\u80fd\u7684 PHP Web \u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u5229\u7528\u534f\u7a0b\u673a\u5236\uff0cSwoole \u6269\u5c55\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5e76\u53d1 I\/O \u64cd\u4f5c\uff0c\u800c\u51fd\u6570\u6269\u5c55\u63d0\u4f9b\u4e86\u5b9a\u5236\u548c\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u7684\u7075\u6d3b\u6027\u3002\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e24\u9879\u6269\u5c55\uff0c\u53ef\u4ee5\u91ca\u653e PHP Web \u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u90e8\u6f5c\u529b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,Swoole\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f \u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01 \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u96c6\u6210\uff0c\u5229\u7528\u534f\u7a0b\u673a\u5236\u5b9e\u73b0\u9ad8\u6027\u80fd PHP \u5e94\u7528\u7a0b\u5e8f\u3002\u51fd\u6570\u6269\u5c55\u63d0\u4f9b\u81ea\u5b9a\u4e49\u529f\u80fd\uff0cSwoole \u6269\u5c55\u63d0\u4f9b\u5f02\u6b65\u5e76\u53d1 I\/O\uff0c\u901a\u8fc7\u96c6\u6210\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728 Swoole \u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\uff0c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4f7f\u7528\u51fd\u6570\u6269\u5c55\u81ea\u5b9a\u4e49 Swoole \u8def\u7531\u89c4\u5219\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u903b\u8f91\u5904\u7406\u8bf7\u6c42\uff0c\u5b9e\u73b0\u7075\u6d3b\u6269\u5c55\u4e0e\u9ad8\u6548\u5e76\u53d1\u3002 PHP \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\uff1a\u534f\u7a0b\u5e26\u6765\u7684\u65b0\u4e16\u754c \u7b80\u4ecb PHP \u51fd\u6570\u6269\u5c55\u662f PHP \u63d0\u4f9b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u81ea\u5df1\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u5e76\u5c06\u5176\u6269\u5c55\u5230 PHP \u4e2d\u3002Swoole \u6269\u5c55\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5e76\u53d1 Web \u670d\u52a1\u5668\u548c\u5f02\u6b65\u7f16\u7a0b\u6846\u67b6\uff0c\u5229\u7528\u534f\u7a0b\u673a\u5236\u5b9e\u73b0\u9ad8\u6027\u80fd\u7f51\u7edc\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5229\u7528\u5b83\u4eec\u521b\u5efa\u9ad8\u6027\u80fd\u7684 PHP Web \u5e94\u7528\u7a0b\u5e8f\u3002 \u534f\u7a0b\u673a\u5236 \u4e86\u89e3\u534f\u7a0b\u662f\u7406\u89e3\u51fd\u6570\u6269\u5c55\u548c Swoole \u6269\u5c55\u5173\u7cfb\u7684\u5173\u952e\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5141\u8bb8\u4ee3\u7801\u5728\u4e0d\u5207\u6362\u5806\u6808\u7684\u60c5\u51b5\u4e0b\u5728\u4e0d\u540c\u7684\u4efb\u52a1\u4e4b\u95f4\u5207\u6362\u3002\u8fd9\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u800c\u65e0\u9700\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002 Swoole \u6269\u5c55\u5229\u7528\u534f\u7a0b\u673a\u5236\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5e76\u53d1 I\/O \u64cd\u4f5c\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u7f16\u5199\u975e\u963b\u585e\u7684\u4ee3\u7801\uff0c\u6700\u5927\u9650\u5ea6\u5730\u5229\u7528 CPU \u8d44\u6e90\u3002 \u51fd\u6570\u6269\u5c55\u4e0e Swoole \u6269\u5c55\u7684\u96c6\u6210 \u51fd\u6570\u6269\u5c55\u53ef\u4ee5\u4e0e Swoole \u6269\u5c55\u96c6\u6210\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728 Swoole [&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-46855","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46855","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=46855"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46855\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}