{"id":47679,"date":"2024-12-02T14:07:17","date_gmt":"2024-12-02T06:07:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47679\/"},"modified":"2024-12-02T14:07:17","modified_gmt":"2024-12-02T06:07:17","slug":"php-%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%9c%a8%e5%a4%a7%e5%9e%8b%e7%b3%bb%e7%bb%9f%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47679\/","title":{"rendered":"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528<\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u80fd\u63d0\u5347\u5927\u578b\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a\u534f\u7a0b\uff1a\u8f7b\u91cf\u7ea7\uff0c\u534f\u4f5c\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4f7f\u7528 Generator \u5b9e\u73b0\u3002\u5e76\u884c\uff1a\u91cd\u91cf\u7ea7\uff0c\u4f7f\u7528\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u51fd\u6570\uff0c\u4f7f\u7528 Process \u548c Thread \u7c7b\u5b9e\u73b0\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729929829671ca2652dc53.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<p>\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u6307\u5c06\u51fd\u6570\u4f5c\u4e3a\u72ec\u7acb\u4efb\u52a1\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u9ad8\u5927\u578b\u7cfb\u7edf\u7684\u6574\u4f53\u6027\u80fd\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5b9e\u73b0\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u4f8b\u5982\u534f\u7a0b\u548c\u5e76\u884c\u3002<\/p>\n<p><strong>\u534f\u7a0b<\/strong><\/p>\n<p>\u534f\u7a0b\u662f PHP \u4e2d\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a PHP \u8fdb\u7a0b\u5185\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u534f\u4f5c\u5e76\u6682\u505c\/\u6062\u590d\u6267\u884c\u3002\u534f\u7a0b\u901a\u8fc7 <code>Generator<\/code> \u5bf9\u8c61\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528\u534f\u7a0b\u5e76\u53d1\u5904\u7406 HTTP \u8bf7\u6c42<\/strong><\/p>\n<pre>use GuzzleHttp\\Client;\n\nfunction fetchPage(string $url): string\n{\n    $client = new Client();\n    return $client-&gt;request('GET', $url)-&gt;getBody()-&gt;getContents();\n}\n\nfunction coroutineHttpRequests(): array\n{\n    $urls = ['https:\/\/example.com', 'https:\/\/example.com\/about', 'https:\/\/example.com\/contact'];\n    $results = [];\n\n    \/\/ Create a generator function\n    function* coroutine() {\n        foreach ($urls as $url) {\n            $result = yield fetchPage($url);\n            $results[] = $result;\n        }\n    }\n\n    \/\/ Spawn the coroutine\n    $coroutine = coroutine();\n\n    \/\/ Iterate over the coroutine and yield results\n    while ($coroutine-&gt;valid()) {\n        $coroutine-&gt;send();\n    }\n\n    return $results;\n}\n\n$results = coroutineHttpRequests();\nprint_r($results);<\/pre>\n<p><strong>\u5e76\u884c<\/strong><\/p>\n<p>\u5e76\u884c\u662f\u4e00\u79cd\u66f4\u91cd\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u4f7f\u7528\u591a\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u51fd\u6570\u3002PHP \u901a\u8fc7 <code>Process<\/code> \u548c <code>Thread<\/code> \u7c7b\u652f\u6301\u5e76\u884c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528\u5e76\u884c\u8fdb\u884c\u6587\u4ef6\u538b\u7f29<\/strong><\/p>\n<pre>use ZipArchive;\n\nfunction compressFile(string $filename): void\n{\n    $zip = new ZipArchive();\n    if ($zip-&gt;open($filename . '.zip', ZipArchive::CREATE) === true) {\n        foreach (glob($filename . '\/*') as $file) {\n            $zip-&gt;addFile($file, basename($file));\n        }\n        $zip-&gt;close();\n    }\n}\n\nfunction parallelFileCompression(): void\n{\n    $files = ['file1', 'file2', 'file3'];\n\n    \/\/ Create a Process object for each file\n    $processes = array_map(function ($file) {\n        return new Process(\"compressFile($file)\");\n    }, $files);\n\n    \/\/ Start the processes\n    foreach ($processes as $process) {\n        $process-&gt;start();\n    }\n\n    \/\/ Wait for the processes to finish\n    foreach ($processes as $process) {\n        $process-&gt;wait();\n    }\n}\n\nparallelFileCompression();<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\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 \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528 \u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u80fd\u63d0\u5347\u5927\u578b\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a\u534f\u7a0b\uff1a\u8f7b\u91cf\u7ea7\uff0c\u534f\u4f5c\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4f7f\u7528 Generator \u5b9e\u73b0\u3002\u5e76\u884c\uff1a\u91cd\u91cf\u7ea7\uff0c\u4f7f\u7528\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u51fd\u6570\uff0c\u4f7f\u7528 Process \u548c Thread \u7c7b\u5b9e\u73b0\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528 \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u6307\u5c06\u51fd\u6570\u4f5c\u4e3a\u72ec\u7acb\u4efb\u52a1\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u9ad8\u5927\u578b\u7cfb\u7edf\u7684\u6574\u4f53\u6027\u80fd\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5b9e\u73b0\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u4f8b\u5982\u534f\u7a0b\u548c\u5e76\u884c\u3002 \u534f\u7a0b \u534f\u7a0b\u662f PHP \u4e2d\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a PHP \u8fdb\u7a0b\u5185\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u534f\u4f5c\u5e76\u6682\u505c\/\u6062\u590d\u6267\u884c\u3002\u534f\u7a0b\u901a\u8fc7 Generator \u5bf9\u8c61\u5b9e\u73b0\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528\u534f\u7a0b\u5e76\u53d1\u5904\u7406 HTTP \u8bf7\u6c42 use GuzzleHttp\\Client; function fetchPage(string $url): string { $client = new Client(); return $client-&gt;request(&#8216;GET&#8217;, $url)-&gt;getBody()-&gt;getContents(); } function coroutineHttpRequests(): array { $urls = [&#8216;https:\/\/example.com&#8217;, &#8216;https:\/\/example.com\/about&#8217;, &#8216;https:\/\/example.com\/contact&#8217;]; $results = []; \/\/ [&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-47679","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47679","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=47679"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47679\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}