{"id":48480,"date":"2024-12-02T10:07:13","date_gmt":"2024-12-02T02:07:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48480\/"},"modified":"2024-12-02T10:07:13","modified_gmt":"2024-12-02T02:07:13","slug":"php%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%92%8c%e4%ba%8b%e4%bb%b6%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48480\/","title":{"rendered":"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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\/1730035768671e403820c55.jpg\" class=\"aligncenter\" title=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u63d2\u56fe\" alt=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\uff0c\u5f02\u6b65\u7f16\u7a0b\u5df2\u6210\u4e3a\u5e94\u5bf9\u9ad8\u5e76\u53d1\u548c\u4f4e\u5ef6\u8fdf\u8bf7\u6c42\u7684\u5fc5\u8981\u65b9\u6cd5\u3002PHP \u7684\u4e8b\u4ef6\u5faa\u73af\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u57fa\u7840\uff0c\u53ef\u8ba9\u60a8\u6784\u5efa\u53ef\u4f38\u7f29\u4e14\u54cd\u5e94\u8fc5\u901f\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u7406\u89e3\u4e8b\u4ef6\u5faa\u73af<\/strong><\/p>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u673a\u5236\uff0c\u5b83\u4e0d\u65ad\u76d1\u63a7\u4e8b\u4ef6\u5e76\u6839\u636e\u9700\u8981\u91c7\u53d6\u76f8\u5e94\u7684\u884c\u52a8\u3002\u5728 PHP \u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u7531 Event Loop \u6269\u5c55\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5f02\u6b65\u7f16\u7a0b<\/strong><\/p>\n<p>\u5f02\u6b65\u7f16\u7a0b\u5141\u8bb8\u60a8\u5904\u7406\u8017\u65f6\u64cd\u4f5c\uff0c\u540c\u65f6\u4fdd\u6301\u670d\u52a1\u5668\u54cd\u5e94\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<pre>use React\\EventLoop\\Loop;\nuse React\\HttpClient\\Client;\n\n\/\/ \u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\n$loop = Loop::get();\n\n\/\/ \u53d1\u9001 HTTP \u8bf7\u6c42\n$client = new Client($loop);\n$client-&gt;request('GET', 'http:\/\/example.com')-&gt;then(function ($response) use ($loop) {\n    \/\/ \u54cd\u5e94\u5904\u7406\u5b8c\u6bd5\u540e\u5173\u95ed\u5faa\u73af\n    $loop-&gt;stop();\n}, function ($e) {\n    \/\/ \u51fa\u9519\u540e\u5173\u95ed\u5faa\u73af\n    $loop-&gt;stop();\n});\n\n\/\/ \u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\n$loop-&gt;run();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u521b\u5efa\u804a\u5929\u670d\u52a1\u5668<\/strong><\/p>\n<pre>use Ratchet\\Server\\IoServer;\nuse Ratchet\\Http\\HttpServer;\nuse Ratchet\\WebSocket\\WsServer;\n\n\/\/ Chat \u6d88\u606f\u5904\u7406\u5668\nclass Chat implements MessageComponentInterface\n{\n    \/\/ \u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\n    protected $clients;\n\n    public function onOpen(ConnectionInterface $conn)\n    {\n        $this-&gt;clients[] = $conn;\n    }\n\n    public function onMessage(ConnectionInterface $from, $msg)\n    {\n        foreach ($this-&gt;clients as $client) {\n            $client-&gt;send($msg);\n        }\n    }\n\n    public function onClose(ConnectionInterface $conn)\n    {\n        \/\/ \u4ece\u5ba2\u6237\u7aef\u96c6\u79fb\u9664\u8fde\u63a5\n    }\n\n    public function onError(ConnectionInterface $conn, \\Exception $e)\n    {\n        \/\/ \u51fa\u9519\u540e\u5173\u95ed\u8fde\u63a5\n    }\n}\n\n\/\/ \u521b\u5efa HTTP \u670d\u52a1\u5668\n$http = new HttpServer(new WsServer(new Chat()));\n\n\/\/ \u521b\u5efa\u5e76\u8fd0\u884c IO \u670d\u52a1\u5668\n$io = new IoServer($http, 8080);\n$io-&gt;run();<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHP \u7684\u4e8b\u4ef6\u5faa\u73af\u63d0\u4f9b\u4e86\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u548c\u6784\u5efa\u53ef\u4f38\u7f29\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\u3002\u901a\u8fc7\u7406\u89e3\u4e8b\u4ef6\u5faa\u73af\u548c\u4f7f\u7528 ReactPHP\u3001Ratchet \u6216\u5176\u4ed6\u5e93\uff0c\u60a8\u53ef\u4ee5\u6784\u5efa\u54cd\u5e94\u8fc5\u901f\u3001\u9ad8\u5e76\u53d1\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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\uff1a\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af \u7b80\u4ecb \u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\uff0c\u5f02\u6b65\u7f16\u7a0b\u5df2\u6210\u4e3a\u5e94\u5bf9\u9ad8\u5e76\u53d1\u548c\u4f4e\u5ef6\u8fdf\u8bf7\u6c42\u7684\u5fc5\u8981\u65b9\u6cd5\u3002PHP \u7684\u4e8b\u4ef6\u5faa\u73af\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u57fa\u7840\uff0c\u53ef\u8ba9\u60a8\u6784\u5efa\u53ef\u4f38\u7f29\u4e14\u54cd\u5e94\u8fc5\u901f\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u7406\u89e3\u4e8b\u4ef6\u5faa\u73af \u4e8b\u4ef6\u5faa\u73af\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u673a\u5236\uff0c\u5b83\u4e0d\u65ad\u76d1\u63a7\u4e8b\u4ef6\u5e76\u6839\u636e\u9700\u8981\u91c7\u53d6\u76f8\u5e94\u7684\u884c\u52a8\u3002\u5728 PHP \u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u7531 Event Loop \u6269\u5c55\u5b9e\u73b0\u3002 \u5f02\u6b65\u7f16\u7a0b \u5f02\u6b65\u7f16\u7a0b\u5141\u8bb8\u60a8\u5904\u7406\u8017\u65f6\u64cd\u4f5c\uff0c\u540c\u65f6\u4fdd\u6301\u670d\u52a1\u5668\u54cd\u5e94\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a use React\\EventLoop\\Loop; use React\\HttpClient\\Client; \/\/ \u521b\u5efa\u4e8b\u4ef6\u5faa\u73af $loop = Loop::get(); \/\/ \u53d1\u9001 HTTP \u8bf7\u6c42 $client = new Client($loop); $client-&gt;request(&#8216;GET&#8217;, &#8216;http:\/\/example.com&#8217;)-&gt;then(function ($response) use ($loop) { \/\/ \u54cd\u5e94\u5904\u7406\u5b8c\u6bd5\u540e\u5173\u95ed\u5faa\u73af $loop-&gt;stop(); }, function ($e) { \/\/ \u51fa\u9519\u540e\u5173\u95ed\u5faa\u73af $loop-&gt;stop(); }); \/\/ \u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af $loop-&gt;run(); [&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-48480","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48480","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=48480"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48480\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}