{"id":24210,"date":"2024-11-21T13:19:05","date_gmt":"2024-11-21T05:19:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24210\/"},"modified":"2024-11-21T13:19:05","modified_gmt":"2024-11-21T05:19:05","slug":"%e4%bd%bf%e7%94%a8swoole%e5%bc%80%e5%8f%91%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84web%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24210\/","title":{"rendered":"\u4f7f\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684Web\u722c\u866b"},"content":{"rendered":"<p>\u4f7f\u7528\u5f00\u53d1\u9ad8\u6027\u80fd\u7684web\u722c\u866b<\/p>\n<p>Web\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u5316\u83b7\u53d6\u7f51\u7edc\u6570\u636e\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5728\u4e92\u8054\u7f51\u4e0a\u6536\u96c6\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u5e94\u7528\u4e8e\u5404\u79cd\u4e0d\u540c\u7684\u9886\u57df\uff0c\u5982\u641c\u7d22\u5f15\u64ce\u3001\u6570\u636e\u5206\u6790\u3001\u7ade\u4e89\u5bf9\u624b\u5206\u6790\u7b49\u3002\u968f\u7740\u4e92\u8054\u7f51\u89c4\u6a21\u548c\u6570\u636e\u91cf\u7684\u5feb\u901f\u589e\u957f\uff0c\u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u6765\u5f00\u53d1\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\uff0c\u5e76\u9644\u4e0a\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662fSwoole\uff1f<br \/>Swoole\u662f\u4e00\u4e2a\u9488\u5bf9PHP\u8bed\u8a00\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u66ff\u4ee3\u539f\u751f\u7684PHP\u6269\u5c55\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u548c\u5f00\u53d1\u6548\u7387\u3002\u5b83\u652f\u6301\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u9ad8\u7f51\u7edc\u901a\u4fe1\u7684\u6548\u7387\u548c\u541e\u5410\u91cf\uff0c\u5e76\u4e14\u5185\u7f6e\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u901a\u4fe1\u76f8\u5173\u7684\u529f\u80fd\u7ec4\u4ef6\uff0c\u5982TCP\/UDP\u670d\u52a1\u5668\u3001HTTP\u670d\u52a1\u5668\u3001WebSocket\u670d\u52a1\u5668\u7b49\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528Swoole\u5f00\u53d1Web\u722c\u866b\u7684\u4f18\u52bf<\/p>\n<ol>\n<li>\u9ad8\u6027\u80fd\uff1aSwoole\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\u53ef\u4ee5\u5145\u5206\u5229\u7528CPU\u548c\u7f51\u7edc\u8d44\u6e90\uff0c\u63d0\u9ad8\u722c\u866b\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/li>\n<li>\u65b9\u4fbf\u6269\u5c55\uff1aSwoole\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u901a\u4fe1\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6269\u5c55\u548c\u5b9a\u5236\u722c\u866b\u7684\u529f\u80fd\u3002<\/li>\n<li>\u5185\u5b58\u7ba1\u7406\uff1aSwoole\u91c7\u7528\u534f\u7a0b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u6709\u6548\u5730\u51cf\u5c11\u5185\u5b58\u7684\u6d88\u8017\u3002<\/li>\n<li>\u591a\u534f\u8bae\u652f\u6301\uff1aSwoole\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5982HTTP\u3001WebSocket\u7b49\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7c7b\u578b\u7684\u722c\u866b\u9700\u6c42\u3002<\/li>\n<\/ol>\n<p>\u4e09\u3001\u4f7f\u7528Swoole\u5f00\u53d1Web\u722c\u866b\u7684\u6b65\u9aa4<br \/>\u6b65\u9aa41\uff1a\u51c6\u5907\u5de5\u4f5c<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u6e90\u7801\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5\u3002\u5177\u4f53\u7684\u5b89\u88c5\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003Swoole\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u6b65\u9aa42\uff1a\u7f16\u5199\u722c\u866b\u4ee3\u7801<br \/>\u4e0b\u9762\u6211\u4eec\u6765\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Web\u722c\u866b\uff0c\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<pre>&lt;?php use SwooleCoroutine;\nuse SwooleCoroutineHttpClient;\n\nclass Spider\n{\n    private $concurrency = 5;   \/\/ \u5e76\u53d1\u6570\u91cf\n    private $urls = [\n        'https:\/\/www.example.com\/page1',\n        'https:\/\/www.example.com\/page2',\n        'https:\/\/www.example.com\/page3',\n        \/\/ \u6dfb\u52a0\u66f4\u591a\u7684URL\n    ];\n\n    public function start()\n    {\n        Coroutineun(function() {\n            $pool = new SplQueue();  \/\/ \u4f7f\u7528\u961f\u5217\u6765\u7ba1\u7406\u5e76\u53d1\u8bf7\u6c42\n            foreach ($this-&gt;urls as $url) {\n                $pool-&amp;gt;push($url);\n            }\n\n            for ($i = 0; $i concurrency; $i++) {\n                Coroutine::create([$this, 'request'], $pool);\n            }\n        });\n    }\n\n    public function request(SplQueue $pool)\n    {\n        while (!$pool-&amp;gt;isEmpty()) {\n            $url = $pool-&amp;gt;shift();\n            $cli = new Client();\n            $cli-&amp;gt;get($url);\n            $response = $cli-&amp;gt;body;\n            \/\/ \u5904\u7406\u54cd\u5e94\u6570\u636e\uff0c\u5982\u89e3\u6790HTML\u3001\u63d0\u53d6\u5185\u5bb9\u7b49\n            \/\/ ...\n            $cli-&amp;gt;close();\n        }\n    }\n}\n\n$spider = new Spider();\n$spider-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u534f\u7a0b\u6765\u5e76\u53d1\u5904\u7406\u8bf7\u6c42\u3002\u5728\u8bf7\u6c42\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684HttpClient\u6765\u53d1\u8d77HTTP\u8bf7\u6c42\uff0c\u5e76\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u8fdb\u884c\u51fd\u6570\u7684\u7f16\u5199\u548c\u4e1a\u52a1\u903b\u8f91\u7684\u5904\u7406\u3002<\/p>\n<p>\u6b65\u9aa43\uff1a\u8fd0\u884c\u722c\u866b<br \/>\u4fdd\u5b58\u4ee5\u4e0a\u4ee3\u7801\u5230\u4e00\u4e2aphp\u6587\u4ef6\u4e2d\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u8be5\u6587\u4ef6\u5373\u53ef\u542f\u52a8\u722c\u866b\u3002<\/p>\n<pre>php spider.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684web\u722c\u866b\u4e86\u3002\u5f53\u7136\uff0c\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u7684\u722c\u866b\u53ef\u80fd\u66f4\u4e3a\u590d\u6742\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<p>\u7ed3\u8bba<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Swoole\u6765\u5f00\u53d1\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\uff0c\u5e76\u9644\u4e0a\u4e86\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u4f7f\u7528Swoole\u53ef\u4ee5\u63d0\u9ad8\u722c\u866b\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u54cd\u5e94\u901f\u5ea6\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u83b7\u53d6\u7f51\u7edc\u6570\u636e\u3002\u5f53\u7136\uff0c\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4e1a\u52a1\u573a\u666f\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\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>\u4f7f\u7528\u5f00\u53d1\u9ad8\u6027\u80fd\u7684web\u722c\u866b Web\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u5316\u83b7\u53d6\u7f51\u7edc\u6570\u636e\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5728\u4e92\u8054\u7f51\u4e0a\u6536\u96c6\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u5e94\u7528\u4e8e\u5404\u79cd\u4e0d\u540c\u7684\u9886\u57df\uff0c\u5982\u641c\u7d22\u5f15\u64ce\u3001\u6570\u636e\u5206\u6790\u3001\u7ade\u4e89\u5bf9\u624b\u5206\u6790\u7b49\u3002\u968f\u7740\u4e92\u8054\u7f51\u89c4\u6a21\u548c\u6570\u636e\u91cf\u7684\u5feb\u901f\u589e\u957f\uff0c\u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u6765\u5f00\u53d1\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\uff0c\u5e76\u9644\u4e0a\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u4ec0\u4e48\u662fSwoole\uff1fSwoole\u662f\u4e00\u4e2a\u9488\u5bf9PHP\u8bed\u8a00\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u66ff\u4ee3\u539f\u751f\u7684PHP\u6269\u5c55\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u548c\u5f00\u53d1\u6548\u7387\u3002\u5b83\u652f\u6301\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u9ad8\u7f51\u7edc\u901a\u4fe1\u7684\u6548\u7387\u548c\u541e\u5410\u91cf\uff0c\u5e76\u4e14\u5185\u7f6e\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u901a\u4fe1\u76f8\u5173\u7684\u529f\u80fd\u7ec4\u4ef6\uff0c\u5982TCP\/UDP\u670d\u52a1\u5668\u3001HTTP\u670d\u52a1\u5668\u3001WebSocket\u670d\u52a1\u5668\u7b49\u3002 \u4e8c\u3001\u4f7f\u7528Swoole\u5f00\u53d1Web\u722c\u866b\u7684\u4f18\u52bf \u9ad8\u6027\u80fd\uff1aSwoole\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\u53ef\u4ee5\u5145\u5206\u5229\u7528CPU\u548c\u7f51\u7edc\u8d44\u6e90\uff0c\u63d0\u9ad8\u722c\u866b\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u54cd\u5e94\u901f\u5ea6\u3002 \u65b9\u4fbf\u6269\u5c55\uff1aSwoole\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u901a\u4fe1\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6269\u5c55\u548c\u5b9a\u5236\u722c\u866b\u7684\u529f\u80fd\u3002 \u5185\u5b58\u7ba1\u7406\uff1aSwoole\u91c7\u7528\u534f\u7a0b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u6709\u6548\u5730\u51cf\u5c11\u5185\u5b58\u7684\u6d88\u8017\u3002 \u591a\u534f\u8bae\u652f\u6301\uff1aSwoole\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5982HTTP\u3001WebSocket\u7b49\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7c7b\u578b\u7684\u722c\u866b\u9700\u6c42\u3002 \u4e09\u3001\u4f7f\u7528Swoole\u5f00\u53d1Web\u722c\u866b\u7684\u6b65\u9aa4\u6b65\u9aa41\uff1a\u51c6\u5907\u5de5\u4f5c\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u6e90\u7801\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5\u3002\u5177\u4f53\u7684\u5b89\u88c5\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003Swoole\u5b98\u65b9\u6587\u6863\u3002 \u6b65\u9aa42\uff1a\u7f16\u5199\u722c\u866b\u4ee3\u7801\u4e0b\u9762\u6211\u4eec\u6765\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Web\u722c\u866b\uff0c\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002 &lt;?php use SwooleCoroutine; use SwooleCoroutineHttpClient; class Spider { private $concurrency = 5; \/\/ \u5e76\u53d1\u6570\u91cf private $urls = [ &#8216;https:\/\/www.example.com\/page1&#8217;, &#8216;https:\/\/www.example.com\/page2&#8217;, &#8216;https:\/\/www.example.com\/page3&#8217;, \/\/ \u6dfb\u52a0\u66f4\u591a\u7684URL ]; public function start() { Coroutineun(function() { $pool = new SplQueue(); \/\/ \u4f7f\u7528\u961f\u5217\u6765\u7ba1\u7406\u5e76\u53d1\u8bf7\u6c42 foreach ($this-&gt;urls as $url) { $pool-&amp;gt;push($url); } for ($i [&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-24210","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24210","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=24210"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24210\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}