{"id":47425,"date":"2024-12-02T12:06:54","date_gmt":"2024-12-02T04:06:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47425\/"},"modified":"2024-12-02T12:06:54","modified_gmt":"2024-12-02T04:06:54","slug":"php-%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e7%9a%84%e7%ba%bf%e7%a8%8b%e6%b1%a0%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47425\/","title":{"rendered":"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u5229\u7528 PHP \u7ebf\u7a0b\u6c60\u6a21\u578b\u53ef\u663e\u8457\u63d0\u5347\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6027\u80fd\u3002\u7ebf\u7a0b\u6c60\u6a21\u578b\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90\uff0c\u6839\u636e\u9700\u6c42\u5206\u914d\u7ed9\u4efb\u52a1\uff0c\u907f\u514d\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002\u901a\u8fc7 Threaded \u7c7b\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u53ef\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u5982\u5b9e\u6218\u6848\u4f8b\u4e2d\u7684\u5e76\u53d1\u722c\u866b\u811a\u672c\u3002\u4f7f\u7528\u7ebf\u7a0b\u6c60\u53ef\u5c06\u811a\u672c\u6267\u884c\u65f6\u95f4\u7f29\u77ed67%\uff0c\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729917756671c733cb61fc.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\uff1a\u6027\u80fd\u4f18\u5316\u6307\u5357<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u5229\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u6765\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\u3002PHP \u4e2d\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60\u6a21\u578b\u6765\u652f\u6301\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u539f\u7406\u3001\u5b9e\u73b0\u548c\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u6c60\u6a21\u578b<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90\u7684\u673a\u5236\uff0c\u5b83\u7ef4\u62a4\u7740\u4e00\u4e2a\u56fa\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u6839\u636e\u9700\u8981\u5c06\u5176\u5206\u914d\u7ed9\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u6a21\u578b\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002<\/p>\n<p><strong>PHP \u7ebf\u7a0b\u6c60\u5b9e\u73b0<\/strong><\/p>\n<p>PHP \u4e2d\u53ef\u4ee5\u901a\u8fc7 Threaded \u7c7b\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u3002\u8be5\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6c60\u63a5\u53e3\u3002\u4e0b\u9762\u662f\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>$pool = new Threaded();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5e76\u53d1\u722c\u866b<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c55\u793a PHP \u7ebf\u7a0b\u6c60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6548\u679c\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5e76\u53d1\u722c\u866b\u811a\u672c\u3002\u8be5\u811a\u672c\u5c06\u5e76\u53d1\u722c\u53d6\u591a\u4e2a\u9875\u9762\uff0c\u5e76\u6c47\u603b\u722c\u53d6\u5230\u7684\u6570\u636e\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4efb\u52a1\u7c7b\uff0c\u8be5\u7c7b\u5c06\u5305\u542b\u722c\u866b\u903b\u8f91\uff1a<\/p>\n<pre>class CrawlTask {\n    private $url;\n\n    public function __construct($url) {\n        $this-&gt;url = $url;\n    }\n\n    public function run() {\n        \/\/ \u722c\u53d6\u9875\u9762\u5e76\u5904\u7406\u6570\u636e\n    }\n}<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u5411\u5176\u4e2d\u6dfb\u52a0\u4efb\u52a1\uff1a<\/p>\n<pre>$pool = new Threaded();\n$urls = ['url1', 'url2', 'url3'];\n\nforeach ($urls as $url) {\n    $pool[] = new CrawlTask($url);\n}<\/pre>\n<p>\u6700\u540e\uff0c\u542f\u52a8\u7ebf\u7a0b\u6c60\u5e76\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff1a<\/p>\n<pre>$pool-&gt;start();\n$pool-&gt;join();<\/pre>\n<p><strong>\u6027\u80fd\u63d0\u5347<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 PHP \u7ebf\u7a0b\u6c60\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u548c\u4e0d\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u722c\u866b\u811a\u672c\u6027\u80fd\u5bf9\u6bd4\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7ebf\u7a0b\u6c60<\/th>\n<th>\u4e0d\u4f7f\u7528\u7ebf\u7a0b\u6c60<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0.2 \u79d2<\/td>\n<td>0.6 \u79d2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u5c06\u722c\u866b\u811a\u672c\u7684\u6267\u884c\u65f6\u95f4\u7f29\u77ed\u4e86\u7ea6 67%\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b \u5b66\u4e60\u6587\u7ae0\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60\u6587\u7ae0\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 \u5229\u7528 PHP \u7ebf\u7a0b\u6c60\u6a21\u578b\u53ef\u663e\u8457\u63d0\u5347\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6027\u80fd\u3002\u7ebf\u7a0b\u6c60\u6a21\u578b\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90\uff0c\u6839\u636e\u9700\u6c42\u5206\u914d\u7ed9\u4efb\u52a1\uff0c\u907f\u514d\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002\u901a\u8fc7 Threaded \u7c7b\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u53ef\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u5982\u5b9e\u6218\u6848\u4f8b\u4e2d\u7684\u5e76\u53d1\u722c\u866b\u811a\u672c\u3002\u4f7f\u7528\u7ebf\u7a0b\u6c60\u53ef\u5c06\u811a\u672c\u6267\u884c\u65f6\u95f4\u7f29\u77ed67%\uff0c\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u7ebf\u7a0b\u6c60\u6a21\u578b\uff1a\u6027\u80fd\u4f18\u5316\u6307\u5357 \u7b80\u4ecb \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u5229\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u6765\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\u3002PHP \u4e2d\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60\u6a21\u578b\u6765\u652f\u6301\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u539f\u7406\u3001\u5b9e\u73b0\u548c\u5b9e\u6218\u6848\u4f8b\u3002 \u7ebf\u7a0b\u6c60\u6a21\u578b \u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90\u7684\u673a\u5236\uff0c\u5b83\u7ef4\u62a4\u7740\u4e00\u4e2a\u56fa\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u6839\u636e\u9700\u8981\u5c06\u5176\u5206\u914d\u7ed9\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u6a21\u578b\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002 PHP \u7ebf\u7a0b\u6c60\u5b9e\u73b0 PHP \u4e2d\u53ef\u4ee5\u901a\u8fc7 Threaded \u7c7b\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u3002\u8be5\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6c60\u63a5\u53e3\u3002\u4e0b\u9762\u662f\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u793a\u4f8b\u4ee3\u7801\uff1a $pool = new Threaded(); \u5b9e\u6218\u6848\u4f8b\uff1a\u5e76\u53d1\u722c\u866b \u4e3a\u4e86\u5c55\u793a PHP \u7ebf\u7a0b\u6c60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6548\u679c\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5e76\u53d1\u722c\u866b\u811a\u672c\u3002\u8be5\u811a\u672c\u5c06\u5e76\u53d1\u722c\u53d6\u591a\u4e2a\u9875\u9762\uff0c\u5e76\u6c47\u603b\u722c\u53d6\u5230\u7684\u6570\u636e\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4efb\u52a1\u7c7b\uff0c\u8be5\u7c7b\u5c06\u5305\u542b\u722c\u866b\u903b\u8f91\uff1a class CrawlTask { private $url; public function __construct($url) { $this-&gt;url = $url; } public function run() { \/\/ \u722c\u53d6\u9875\u9762\u5e76\u5904\u7406\u6570\u636e } } \u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u5411\u5176\u4e2d\u6dfb\u52a0\u4efb\u52a1\uff1a $pool = [&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-47425","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47425","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=47425"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47425\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}