{"id":23349,"date":"2024-11-21T11:29:29","date_gmt":"2024-11-21T03:29:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23349\/"},"modified":"2024-11-21T11:29:29","modified_gmt":"2024-11-21T03:29:29","slug":"swoole%e8%bf%9b%e9%98%b6%ef%bc%9a%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e8%bf%9b%e8%a1%8cweb%e7%88%ac%e8%99%ab%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23349\/","title":{"rendered":"Swoole\u8fdb\u9636\uff1a\u4f7f\u7528\u534f\u7a0b\u8fdb\u884cWeb\u722c\u866b\u5f00\u53d1"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cweb\u722c\u866b\u5df2\u7ecf\u6210\u4e3a\u5f53\u4eca\u4e92\u8054\u7f51\u5e94\u7528\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5176\u5728\u6570\u636e\u91c7\u96c6\u3001\u4e1a\u52a1\u53d1\u6398\u3001\u8206\u60c5\u76d1\u6d4b\u7b49\u65b9\u9762\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u7136\u800c\u4f20\u7edf\u7684web\u722c\u866b\u901a\u5e38\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\u8bf7\u6c42\uff0c\u9762\u4e34\u7684\u95ee\u9898\u5305\u62ec\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3001\u5185\u5b58\u5360\u7528\u8fc7\u5927\u7b49\u3002\u800c\u8fd1\u5e74\u6765\uff0c\u6210\u4e3aphp\u5e94\u7528\u4e2d\u7684\u4e00\u9897\u65b0\u661f\uff0c\u5b83\u7684\u534f\u7a0b\u7279\u6027\u53ef\u4ee5\u4e3aweb\u722c\u866b\u7684\u5e76\u53d1\u8bf7\u6c42\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u534f\u7a0b\u5b9e\u73b0\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6548\u7684Web\u722c\u866b\u3002<\/p>\n<h2>Swoole\u7b80\u4ecb<\/h2>\n<p>Swoole\u662f\u57fa\u4e8ePHP\u8bed\u8a00\u5b9e\u73b0\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5176\u6700\u5927\u7684\u7279\u70b9\u662f\u652f\u6301\u534f\u7a0b\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u7528\u6237\u6001\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u4e0e\u4f20\u7edf\u7684\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u76f8\u6bd4\uff0c\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u5c0f\u3001\u5185\u5b58\u5360\u7528\u5c11\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u53d1\u6325CPU\u7684\u6027\u80fd\u3002<\/p>\n<h2>\u4f7f\u7528Swoole\u5b9e\u73b0Web\u722c\u866b<\/h2>\n<p>Swoole\u7684\u534f\u7a0b\u7279\u6027\u4e3aWeb\u722c\u866b\u7684\u5f00\u53d1\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u5e73\u53f0\u3002\u4f20\u7edf\u7684Web\u722c\u866b\u5728\u5e76\u53d1\u8bf7\u6c42\u65f6\u5f80\u5f80\u9700\u8981\u6d88\u8017\u5927\u91cf\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u800c\u4f7f\u7528Swoole\u534f\u7a0b\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u9ad8\u5e76\u53d1\u8bf7\u6c42\uff0c\u540c\u65f6\u8fd8\u80fd\u907f\u514d\u4f20\u7edf\u7684\u7ebf\u7a0b\u5207\u6362\u5e26\u6765\u7684\u5f00\u9500\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Swoole\u5b9e\u73b0\u7684Web\u722c\u866b\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php \/\/ 1. \u521b\u5efaSwoole HTTP\u670d\u52a1\u5668\n$http = new SwooleHttpServer(\"0.0.0.0\", 9501);\n\n\/\/ 2. \u5904\u7406\u8bf7\u6c42\n$http-&gt;on('request', function ($request, $response) {\n    \/\/ 3. \u53d1\u9001HTTP\u8bf7\u6c42\n    $cli = new SwooleCoroutineHttpClient('www.baidu.com', 80);\n    $cli-&amp;gt;setHeaders([\n        'Host' =&amp;gt; \"www.baidu.com\",\n        \"User-Agent\" =&amp;gt; 'Chrome\/49.0.2587.3',\n        'Accept' =&amp;gt; 'text\/html,application\/xhtml+xml,application\/xml',\n        'Accept-Encoding' =&amp;gt; 'gzip',\n    ]);\n    $cli-&amp;gt;get('\/');\n\n    \/\/ 4. \u54cd\u5e94HTML\u5185\u5bb9\n    $response-&amp;gt;header(\"Content-Type\", \"text\/html; charset=utf-8\");\n    $response-&amp;gt;end($cli-&amp;gt;body);\n});\n\n\/\/ 5. \u542f\u52a8HTTP\u670d\u52a1\u5668\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aSwoole HTTP\u670d\u52a1\u5668\uff0c\u76d1\u542c\u7aef\u53e3\u53f79501\u3002\u5f53\u6709HTTP\u8bf7\u6c42\u5230\u8fbe\u65f6\uff0c\u670d\u52a1\u5668\u5c06\u53d1\u9001HTTP\u8bf7\u6c42\u5230\u767e\u5ea6\u7f51\u7ad9\uff0c\u5e76\u54cd\u5e94HTML\u5185\u5bb9\u3002<\/p>\n<h2>Swoole\u534f\u7a0bHTTP\u5ba2\u6237\u7aef<\/h2>\n<p>Swoole\u63d0\u4f9b\u4e86\u57fa\u4e8e\u534f\u7a0b\u7684HTTP\u5ba2\u6237\u7aef\uff0c\u901a\u8fc7\u534f\u7a0b\u53ef\u4ee5\u5728\u5355\u4e2a\u8fdb\u7a0b\u91cc\u9762\u540c\u65f6\u53d1\u8d77\u591a\u4e2aHTTP\u8bf7\u6c42\uff0c\u5e76\u884c\u6267\u884c\u8bf7\u6c42\uff0c\u800c\u65e0\u9700\u5f00\u542f\u591a\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u3002<\/p>\n<p>\u534f\u7a0bHTTP\u5ba2\u6237\u7aef\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php \/\/ 1. \u521b\u5efa\u534f\u7a0bHTTP\u5ba2\u6237\u7aef\n$cli = new SwooleCoroutineHttpClient('www.baidu.com', 80);\n\n\/\/ 2. \u914d\u7f6e\u8bf7\u6c42\u5934\n$cli-&gt;setHeaders([\n    'Host' =&amp;gt; \"www.baidu.com\",\n    \"User-Agent\" =&amp;gt; 'Chrome\/49.0.2587.3',\n    'Accept' =&amp;gt; 'text\/html,application\/xhtml+xml,application\/xml',\n    'Accept-Encoding' =&amp;gt; 'gzip',\n]);\n\n\/\/ 3. \u53d1\u9001HTTP\u8bf7\u6c42\n$cli-&amp;gt;get('\/');\n\n\/\/ 4. \u8f93\u51fa\u54cd\u5e94\u5185\u5bb9\necho $cli-&amp;gt;body;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u534f\u7a0bHTTP\u5ba2\u6237\u7aef\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u5934\u540e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u8f93\u51fa\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<h2>\u8fd0\u7528\u534f\u7a0b\u5b9e\u73b0\u722c\u866b\u722c\u53d6<\/h2>\n<p>\u4f7f\u7528Swoole\u534f\u7a0bHTTP\u5ba2\u6237\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684Web\u722c\u866b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u7684\u722c\u866b\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php \/\/ 1. \u6293\u53d6\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u7684\u9875\u9762\n$html = file_get_contents('https:\/\/www.baidu.com\/s?ie=UTF-8&amp;wd=swoole');\n\n\/\/ 2. \u89e3\u6790HTML\uff0c\u63d0\u53d6\u641c\u7d22\u7ed3\u679c\u5217\u8868\u7684URL\npreg_match_all('\/&lt;a.*?href=\"(.*?)\".*?&gt;\/is', $html, $matches);\n$urls = $matches[1];\n\n\/\/ 3. \u5e76\u53d1\u8bf7\u6c42\u641c\u7d22\u7ed3\u679c\u5217\u8868\u7684URL\n$cli = new SwooleCoroutineHttpClient('www.baidu.com', 80);\nforeach ($urls as $url) {\n    $cli-&amp;gt;setHeaders([\n        'Host' =&amp;gt; \"www.baidu.com\",\n        \"User-Agent\" =&amp;gt; 'Chrome\/49.0.2587.3',\n        'Accept' =&amp;gt; 'text\/html,application\/xhtml+xml,application\/xml',\n        'Accept-Encoding' =&amp;gt; 'gzip',\n    ]);\n    $cli-&amp;gt;get($url);\n    echo $cli-&amp;gt;body;\n}\n\n\/\/ 4. \u5173\u95edHTTP\u5ba2\u6237\u7aef\n$cli-&amp;gt;close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\u9996\u5148\u6293\u53d6\u767e\u5ea6\u641c\u7d22\u201cswoole\u201d\u5173\u952e\u5b57\u7684\u9875\u9762\uff0c\u5e76\u89e3\u6790HTML\uff0c\u63d0\u53d6\u641c\u7d22\u7ed3\u679c\u5217\u8868\u7684URL\uff0c\u5e76\u5e76\u53d1\u8bf7\u6c42\u8fd9\u4e9bURL\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Swoole\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5176\u534f\u7a0b\u7279\u6027\u4e3aWeb\u722c\u866b\u7684\u5f00\u53d1\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4f7f\u7528Swoole\u534f\u7a0bHTTP\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u5347Web\u722c\u866b\u7684\u5e76\u53d1\u8bf7\u6c42\u80fd\u529b\uff0c\u540c\u65f6\u907f\u514d\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u5e26\u6765\u7684\u8d44\u6e90\u6d88\u8017\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u8fdb\u9636\uff1a\u4f7f\u7528\u534f\u7a0b\u8fdb\u884cWeb\u722c\u866b\u5f00\u53d1\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>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cweb\u722c\u866b\u5df2\u7ecf\u6210\u4e3a\u5f53\u4eca\u4e92\u8054\u7f51\u5e94\u7528\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5176\u5728\u6570\u636e\u91c7\u96c6\u3001\u4e1a\u52a1\u53d1\u6398\u3001\u8206\u60c5\u76d1\u6d4b\u7b49\u65b9\u9762\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u7136\u800c\u4f20\u7edf\u7684web\u722c\u866b\u901a\u5e38\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\u8bf7\u6c42\uff0c\u9762\u4e34\u7684\u95ee\u9898\u5305\u62ec\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3001\u5185\u5b58\u5360\u7528\u8fc7\u5927\u7b49\u3002\u800c\u8fd1\u5e74\u6765\uff0c\u6210\u4e3aphp\u5e94\u7528\u4e2d\u7684\u4e00\u9897\u65b0\u661f\uff0c\u5b83\u7684\u534f\u7a0b\u7279\u6027\u53ef\u4ee5\u4e3aweb\u722c\u866b\u7684\u5e76\u53d1\u8bf7\u6c42\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u534f\u7a0b\u5b9e\u73b0\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6548\u7684Web\u722c\u866b\u3002 Swoole\u7b80\u4ecb Swoole\u662f\u57fa\u4e8ePHP\u8bed\u8a00\u5b9e\u73b0\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5176\u6700\u5927\u7684\u7279\u70b9\u662f\u652f\u6301\u534f\u7a0b\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u7528\u6237\u6001\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u4e0e\u4f20\u7edf\u7684\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u76f8\u6bd4\uff0c\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u5c0f\u3001\u5185\u5b58\u5360\u7528\u5c11\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u53d1\u6325CPU\u7684\u6027\u80fd\u3002 \u4f7f\u7528Swoole\u5b9e\u73b0Web\u722c\u866b Swoole\u7684\u534f\u7a0b\u7279\u6027\u4e3aWeb\u722c\u866b\u7684\u5f00\u53d1\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u5e73\u53f0\u3002\u4f20\u7edf\u7684Web\u722c\u866b\u5728\u5e76\u53d1\u8bf7\u6c42\u65f6\u5f80\u5f80\u9700\u8981\u6d88\u8017\u5927\u91cf\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u800c\u4f7f\u7528Swoole\u534f\u7a0b\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u9ad8\u5e76\u53d1\u8bf7\u6c42\uff0c\u540c\u65f6\u8fd8\u80fd\u907f\u514d\u4f20\u7edf\u7684\u7ebf\u7a0b\u5207\u6362\u5e26\u6765\u7684\u5f00\u9500\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Swoole\u5b9e\u73b0\u7684Web\u722c\u866b\u793a\u4f8b\uff1a &lt;?php \/\/ 1. \u521b\u5efaSwoole HTTP\u670d\u52a1\u5668 $http = new SwooleHttpServer(&#8220;0.0.0.0&#8221;, 9501); \/\/ 2. \u5904\u7406\u8bf7\u6c42 $http-&gt;on(&#8216;request&#8217;, function ($request, $response) { \/\/ 3. \u53d1\u9001HTTP\u8bf7\u6c42 $cli = new SwooleCoroutineHttpClient(&#8216;www.baidu.com&#8217;, 80); $cli-&amp;gt;setHeaders([ &#8216;Host&#8217; =&amp;gt; &#8220;www.baidu.com&#8221;, &#8220;User-Agent&#8221; =&amp;gt; &#8216;Chrome\/49.0.2587.3&#8217;, &#8216;Accept&#8217; =&amp;gt; &#8216;text\/html,application\/xhtml+xml,application\/xml&#8217;, &#8216;Accept-Encoding&#8217; =&amp;gt; &#8216;gzip&#8217;, ]); $cli-&amp;gt;get(&#8216;\/&#8217;); \/\/ 4. \u54cd\u5e94HTML\u5185\u5bb9 $response-&amp;gt;header(&#8220;Content-Type&#8221;, &#8220;text\/html; charset=utf-8&#8221;); $response-&amp;gt;end($cli-&amp;gt;body); [&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-23349","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23349","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=23349"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23349\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}