{"id":47722,"date":"2024-12-02T11:02:53","date_gmt":"2024-12-02T03:02:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47722\/"},"modified":"2024-12-02T11:02:53","modified_gmt":"2024-12-02T03:02:53","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-go-%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47722\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>PHP \u548c Go \u53ef\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5b9e\u73b0\u4f18\u52bf\u4e92\u8865\uff0cPHP \u64c5\u957f\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800c Go \u5177\u6709\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u6027\u3002\u901a\u8fc7 Go \u521b\u5efa\u670d\u52a1\uff0c\u5e76\u901a\u8fc7 PHP \u53d1\u51fa\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bed\u8a00\u95f4\u7684\u4ea4\u4e92\u3002\u5b9e\u6218\u4e2d\uff0cPHP \u5e94\u7528\u7a0b\u5e8f\u53ef\u901a\u8fc7 curl \u8bf7\u6c42\u8bbf\u95ee\u7531 Go \u7f16\u5199\u5e76\u90e8\u7f72\u7684 API\uff0c\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729932207671cabaf13885.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 PHP \u4e0e Go \u4ea4\u4e92<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u548c Go \u662f\u4e24\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4ee5\u521b\u5efa\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u3002PHP \u64c5\u957f\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800c Go \u5219\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u7279\u6027\u800c\u95fb\u540d\u3002\u901a\u8fc7\u5c06\u4e24\u8005\u7ed3\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e24\u8005\u7684\u4f18\u52bf\u3002<\/p>\n<p><strong>\u5efa\u7acb Go \u670d\u52a1<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728 Go \u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>main.go<\/code> \u7684\u6587\u4ef6\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n)\n\nfunc main() {\n    http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\n        fmt.Fprintln(w, \"Hello, PHP!\")\n    })\n    http.ListenAndServe(\":8080\", nil)\n}<\/pre>\n<p>\u8fd0\u884c\u6b64\u670d\u52a1\uff1a<\/p>\n<pre>go run main.go<\/pre>\n<p>\u8fd9\u5c06\u5728\u7aef\u53e3 8080 \u4e0a\u542f\u52a8\u4e00\u4e2a Go \u670d\u52a1\uff0c\u5f53\u63a5\u6536\u5230 HTTP \u8bf7\u6c42\u65f6\uff0c\u5b83\u4f1a\u54cd\u5e94 &#8220;Hello, PHP!&#8221;\u3002<\/p>\n<p><strong>\u4f7f\u7528 PHP \u53d1\u51fa\u8bf7\u6c42<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528 PHP \u5411 Go \u670d\u52a1\u53d1\u51fa\u8bf7\u6c42\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>index.php<\/code> \u7684\u6587\u4ef6\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php\n$ch = curl_init();\n\ncurl_setopt($ch, CURLOPT_URL, 'http:\/\/localhost:8080');\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n\n$response = curl_exec($ch);\n\necho $response;<\/pre>\n<p>\u8fd0\u884c\u6b64 PHP \u811a\u672c\uff1a<\/p>\n<pre>php index.php<\/pre>\n<p>\u8fd9\u5c06\u5411 Go \u670d\u52a1\u53d1\u51fa\u4e00\u4e2a HTTP \u8bf7\u6c42\u5e76\u6253\u5370\u5176\u54cd\u5e94\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4ece PHP \u8bbf\u95ee Go API<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u573a\u666f\uff1a\u6211\u4eec\u6709\u4e00\u4e2a PHP \u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u7531 Go \u7f16\u5199\u548c\u90e8\u7f72\u7684 API\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u6280\u672f\u6765\u5b9e\u73b0\u6b64\u96c6\u6210\u3002<\/p>\n<p><strong>Go API<\/strong><\/p>\n<p>\u6211\u4eec\u7684 Go API \u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\u5e76\u8fd4\u56de\u5176\u53cd\u8f6c\u503c\u3002<code>main.go<\/code> \u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre>func reverse(s string) string {\n    runes := []rune(s)\n    for i, j := 0, len(runes)-1; i &lt; len(runes)\/2; i, j = i+1, j-1 {\n        runes[i], runes[j] = runes[j], runes[i]\n    }\n    return string(runes)\n}<\/pre>\n<p><strong>PHP \u5ba2\u6237\u7aef<\/strong><\/p>\n<p>\u4f7f\u7528\u524d\u9762\u7684 <code>curl<\/code> \u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 PHP \u5ba2\u6237\u7aef\u4e0e\u6b64 API \u4ea4\u4e92\uff1a<\/p>\n<pre>$url = 'http:\/\/localhost:8080\/reverse?s=' . $string;\n$response = curl_exec($ch);\n\necho $response;<\/pre>\n<p>\u8fd9\u5c06\u5411 Go API \u53d1\u51fa\u8bf7\u6c42\uff0c\u53cd\u8f6c\u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92 \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Go \u4ea4\u4e92\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 PHP \u548c Go \u53ef\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5b9e\u73b0\u4f18\u52bf\u4e92\u8865\uff0cPHP \u64c5\u957f\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800c Go \u5177\u6709\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u6027\u3002\u901a\u8fc7 Go \u521b\u5efa\u670d\u52a1\uff0c\u5e76\u901a\u8fc7 PHP \u53d1\u51fa\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bed\u8a00\u95f4\u7684\u4ea4\u4e92\u3002\u5b9e\u6218\u4e2d\uff0cPHP \u5e94\u7528\u7a0b\u5e8f\u53ef\u901a\u8fc7 curl \u8bf7\u6c42\u8bbf\u95ee\u7531 Go \u7f16\u5199\u5e76\u90e8\u7f72\u7684 API\uff0c\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u3002 \u4f7f\u7528 PHP \u4e0e Go \u4ea4\u4e92 \u7b80\u4ecb PHP \u548c Go \u662f\u4e24\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4ee5\u521b\u5efa\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u3002PHP \u64c5\u957f\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800c Go \u5219\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u7279\u6027\u800c\u95fb\u540d\u3002\u901a\u8fc7\u5c06\u4e24\u8005\u7ed3\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e24\u8005\u7684\u4f18\u52bf\u3002 \u5efa\u7acb Go \u670d\u52a1 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728 Go \u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a main.go \u7684\u6587\u4ef6\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a package main import ( &#8220;fmt&#8221; [&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-47722","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47722","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=47722"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47722\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}