{"id":48326,"date":"2024-12-02T09:14:25","date_gmt":"2024-12-02T01:14:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48326\/"},"modified":"2024-12-02T09:14:25","modified_gmt":"2024-12-02T01:14:25","slug":"php-%e5%87%bd%e6%95%b0%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e9%ab%98%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48326\/","title":{"rendered":"PHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>PHP \u7f51\u7edc\u7f16\u7a0b\u51ed\u501f\u5176\u51fa\u8272\u7684\u7f51\u7edc\u4ea4\u4e92\u80fd\u529b\u800c\u8457\u79f0\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u7684 PHP \u7f51\u7edc\u7f16\u7a0b\u6280\u672f\u3002\u57fa\u7840\u77e5\u8bc6\u5305\u62ec\uff1a\u521b\u5efa\u5957\u63a5\u5b57\u8fde\u63a5\u548c\u65ad\u5f00\u8fde\u63a5\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u9ad8\u7ea7\u6982\u5ff5\u5305\u62ec\uff1a\u8d85\u65f6\u8bbe\u7f6e\u975e\u963b\u585e\u64cd\u4f5c\u5f02\u6b65\u7f16\u7a0b\uff0c\u4f8b\u5982\u4f7f\u7528\u534f\u7a0b\u5904\u7406\u5e76\u53d1\u8fde\u63a5<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730014366671dec9ee875c.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7<\/strong><\/p>\n<p>PHP \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\uff0c\u56e0\u4e3a\u5b83\u5177\u6709\u51fa\u8272\u7684\u7f51\u7edc\u7f16\u7a0b\u80fd\u529b\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u4e0e\u7f51\u7edc\u8d44\u6e90\u8fdb\u884c\u4ea4\u4e92\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 PHP \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u9010\u6b65\u5411\u9ad8\u7ea7\u6982\u5ff5\u8fc8\u8fdb\u3002<\/p>\n<p><strong>\u57fa\u7840\u77e5\u8bc6<\/strong><\/p>\n<ul>\n<li><strong><code>socket_create()<\/code>\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u3002<\/li>\n<li><strong><code>socket_connect()<\/code>\uff1a<\/strong>\u5c06\u5957\u63a5\u5b57\u8fde\u63a5\u5230\u6307\u5b9a\u7684\u4e3b\u673a\u548c\u7aef\u53e3\u3002<\/li>\n<li><strong><code>socket_write()<\/code>\uff1a<\/strong>\u5411\u5957\u63a5\u5b57\u5199\u5165\u6570\u636e\u3002<\/li>\n<li><strong><code>socket_read()<\/code>\uff1a<\/strong>\u4ece\u5957\u63a5\u5b57\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<li><strong><code>socket_close()<\/code>\uff1a<\/strong>\u5173\u95ed\u5957\u63a5\u5b57\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p><strong>\u6848\u4f8b 1\uff1a\u4e0e\u8fdc\u7a0b\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5<\/strong><\/p>\n<pre>&lt;?php\n\/\/ \u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\n$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);\n\n\/\/ \u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\nsocket_connect($socket, 'example.com', 80);\n\n\/\/ \u53d1\u9001\u4e00\u4e2a HTTP \u8bf7\u6c42\nsocket_write($socket, \"GET \/ HTTP\/1.1\\r\\n\");\n\n\/\/ \u63a5\u6536\u670d\u52a1\u5668\u54cd\u5e94\n$response = socket_read($socket, 1024);\n\n\/\/ \u8f93\u51fa\u670d\u52a1\u5668\u54cd\u5e94\necho $response;\n\n\/\/ \u5173\u95ed\u5957\u63a5\u5b57\u8fde\u63a5\nsocket_close($socket);\n?&gt;<\/pre>\n<p><strong>\u9ad8\u7ea7\u6982\u5ff5<\/strong><\/p>\n<ul>\n<li><strong>\u8d85\u65f6\uff1a<\/strong>\u4e3a\u5957\u63a5\u5b57\u64cd\u4f5c\u8bbe\u7f6e\u8d85\u65f6\u3002<\/li>\n<li><strong>\u975e\u963b\u585e\u64cd\u4f5c\uff1a<\/strong>\u4f7f\u5957\u63a5\u5b57\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u65e0\u9700\u7b49\u5f85\u54cd\u5e94\u3002<\/li>\n<li><strong>\u5f02\u6b65\u7f16\u7a0b\uff1a<\/strong>\u4f7f\u7528\u534f\u7a0b\u6216\u4e8b\u4ef6\u5faa\u73af\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p><strong>\u6848\u4f8b 2\uff1a\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528 Swoole \u6269\u5c55\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff1a<\/p>\n<pre>&lt;?php\nuse Swoole\\Coroutine\\Socket;\n\n\/\/ \u521b\u5efa\u534f\u7a0b\u6c60\n$pool = new Swoole\\Coroutine\\Pool(10);\n\n\/\/ \u521b\u5efa\u4e00\u4e2a Swoole \u5957\u63a5\u5b57\u5bf9\u8c61\n$socket = new Socket(AF_INET, SOCK_STREAM, SOL_TCP);\n\n\/\/ \u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\n$socket-&gt;connect('example.com', 80);\n\n\/\/ \u5c06\u5957\u63a5\u5b57\u6dfb\u52a0\u5230\u534f\u7a0b\u6c60\u4e2d\n$pool-&gt;add($socket);\n\n\/\/ \u5faa\u73af\u5904\u7406\u534f\u7a0b\nwhile (true) {\n    \/\/ \u83b7\u53d6\u534f\u7a0b\u5217\u8868\n    $coroutines = $pool-&gt;getCoroutines();\n\n    \/\/ \u904d\u5386\u6bcf\u4e2a\u534f\u7a0b\n    foreach ($coroutines as $coroutine) {\n        \/\/ \u68c0\u67e5\u534f\u7a0b\u662f\u5426\u51c6\u5907\u597d\u5199\u5165\u6570\u636e\n        if ($coroutine-&gt;isWritable()) {\n            $coroutine-&gt;write('GET \/ HTTP\/1.1\\r\\n');\n        }\n        \/\/ \u68c0\u67e5\u534f\u7a0b\u662f\u5426\u51c6\u5907\u597d\u8bfb\u53d6\u6570\u636e\n        else if ($coroutine-&gt;isReadable()) {\n            $response = $coroutine-&gt;read();\n            echo $response;\n        }\n    }\n\n    \/\/ \u8ba9\u534f\u7a0b\u6c60\u6267\u884c\n    $pool-&gt;cycle();\n}\n?&gt;<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 PHP \u7684\u7f51\u7edc\u7f16\u7a0b\u51fd\u6570\u548c\u9ad8\u7ea7\u6982\u5ff5\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u9ad8\u6548\u548c\u53ef\u4f38\u7f29\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,\u7f51\u7edc\u7f16\u7a0b\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7 \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u51fd\u6570\u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 PHP \u7f51\u7edc\u7f16\u7a0b\u51ed\u501f\u5176\u51fa\u8272\u7684\u7f51\u7edc\u4ea4\u4e92\u80fd\u529b\u800c\u8457\u79f0\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u7684 PHP \u7f51\u7edc\u7f16\u7a0b\u6280\u672f\u3002\u57fa\u7840\u77e5\u8bc6\u5305\u62ec\uff1a\u521b\u5efa\u5957\u63a5\u5b57\u8fde\u63a5\u548c\u65ad\u5f00\u8fde\u63a5\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u9ad8\u7ea7\u6982\u5ff5\u5305\u62ec\uff1a\u8d85\u65f6\u8bbe\u7f6e\u975e\u963b\u585e\u64cd\u4f5c\u5f02\u6b65\u7f16\u7a0b\uff0c\u4f8b\u5982\u4f7f\u7528\u534f\u7a0b\u5904\u7406\u5e76\u53d1\u8fde\u63a5 PHP \u7f51\u7edc\u7f16\u7a0b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7 PHP \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\uff0c\u56e0\u4e3a\u5b83\u5177\u6709\u51fa\u8272\u7684\u7f51\u7edc\u7f16\u7a0b\u80fd\u529b\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u4e0e\u7f51\u7edc\u8d44\u6e90\u8fdb\u884c\u4ea4\u4e92\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 PHP \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u9010\u6b65\u5411\u9ad8\u7ea7\u6982\u5ff5\u8fc8\u8fdb\u3002 \u57fa\u7840\u77e5\u8bc6 socket_create()\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5957\u63a5\u5b57\u3002 socket_connect()\uff1a\u5c06\u5957\u63a5\u5b57\u8fde\u63a5\u5230\u6307\u5b9a\u7684\u4e3b\u673a\u548c\u7aef\u53e3\u3002 socket_write()\uff1a\u5411\u5957\u63a5\u5b57\u5199\u5165\u6570\u636e\u3002 socket_read()\uff1a\u4ece\u5957\u63a5\u5b57\u8bfb\u53d6\u6570\u636e\u3002 socket_close()\uff1a\u5173\u95ed\u5957\u63a5\u5b57\u8fde\u63a5\u3002 \u6848\u4f8b 1\uff1a\u4e0e\u8fdc\u7a0b\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5 &lt;?php \/\/ \u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); \/\/ \u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668 socket_connect($socket, &#8216;example.com&#8217;, 80); \/\/ \u53d1\u9001\u4e00\u4e2a HTTP \u8bf7\u6c42 socket_write($socket, &#8220;GET \/ HTTP\/1.1\\r\\n&#8221;); \/\/ \u63a5\u6536\u670d\u52a1\u5668\u54cd\u5e94 $response = socket_read($socket, 1024); \/\/ \u8f93\u51fa\u670d\u52a1\u5668\u54cd\u5e94 echo $response; \/\/ [&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-48326","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48326","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=48326"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48326\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}