{"id":47886,"date":"2024-12-02T16:23:12","date_gmt":"2024-12-02T08:23:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47886\/"},"modified":"2024-12-02T16:23:12","modified_gmt":"2024-12-02T08:23:12","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-python-%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47886\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>PHP \u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e0e Python \u4ea4\u4e92\uff1a\u521b\u5efa\u5305\u542b Python \u547d\u4ee4\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 shell_exec \u51fd\u6570\u6267\u884c\u547d\u4ee4\u3002\u4ece subprocess \u83b7\u53d6\u8f93\u51fa\u5e76\u5c06\u5176\u56de\u663e\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729941505671cd001d57ae.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92<\/h2>\n<p>PHP \u548c Python \u90fd\u662f\u5e7f\u4e3a\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u6709\u65f6\u9700\u8981\u5b83\u4eec\u76f8\u4e92\u6c9f\u901a\u3002\u867d\u7136\u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u5177\u6709\u6311\u6218\u6027\uff0c\u4f46\u5b9e\u9645\u4e0a\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa subprocess \u5e76\u4f7f\u7528 sys \u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b Python \u811a\u672c <code>my_python_script.py<\/code>\uff1a<\/p>\n<pre>def add_numbers(a, b):\n  return a + b<\/pre>\n<p>\u8981\u4ece PHP \u8c03\u7528\u6b64\u811a\u672c\u5e76\u6c42\u548c\u4e24\u4e2a\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b PHP \u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php\n\n\/\/ \u521b\u5efa\u547d\u4ee4\u5b57\u7b26\u4e32\n$command = 'python3 my_python_script.py ' . $_GET['num1'] . ' ' . $_GET['num2'];\n\n\/\/ \u6267\u884c\u547d\u4ee4\n$output = shell_exec($command);\n\n\/\/ \u8f93\u51fa\u7ed3\u679c\necho $output;\n\n?&gt;<\/pre>\n<p><strong>\u4ee3\u7801\u89e3\u91ca<\/strong><\/p>\n<ol>\n<li><code>shell_exec<\/code> \u51fd\u6570\u521b\u5efa subprocess \u5e76\u6267\u884c\u547d\u4ee4\u3002<\/li>\n<li>\u8f93\u5165\u6570\u636e\u4ece GET \u53c2\u6570\u4e2d\u83b7\u53d6\uff0c\u5e76\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u811a\u672c\u3002<\/li>\n<li>\u8f93\u51fa\u5b58\u50a8\u5728 <code>$output<\/code> \u4e2d\uff0c\u5e76\u5c06\u5176\u56de\u663e\u5230\u5c4f\u5e55\u4e0a\u3002<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd Python \u89e3\u91ca\u5668\u5b89\u88c5\u5728\u670d\u52a1\u5668\u4e0a\u5e76\u5df2\u6dfb\u52a0\u5230 PATH \u4e2d\u3002<\/li>\n<li>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8c28\u614e\u4f7f\u7528 <code>shell_exec<\/code>\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528 dedicated PHP package \u800c\u4e0d\u662f subprocess\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92 \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP \u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e0e Python \u4ea4\u4e92\uff1a\u521b\u5efa\u5305\u542b Python \u547d\u4ee4\u7684\u547d\u4ee4\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 shell_exec \u51fd\u6570\u6267\u884c\u547d\u4ee4\u3002\u4ece subprocess \u83b7\u53d6\u8f93\u51fa\u5e76\u5c06\u5176\u56de\u663e\u5230\u5c4f\u5e55\u4e0a\u3002 PHP \u51fd\u6570\u5982\u4f55\u4e0e Python \u4ea4\u4e92 PHP \u548c Python \u90fd\u662f\u5e7f\u4e3a\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u6709\u65f6\u9700\u8981\u5b83\u4eec\u76f8\u4e92\u6c9f\u901a\u3002\u867d\u7136\u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u5177\u6709\u6311\u6218\u6027\uff0c\u4f46\u5b9e\u9645\u4e0a\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa subprocess \u5e76\u4f7f\u7528 sys \u6a21\u5757\u5b9e\u73b0\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b Python \u811a\u672c my_python_script.py\uff1a def add_numbers(a, b): return a + b \u8981\u4ece PHP \u8c03\u7528\u6b64\u811a\u672c\u5e76\u6c42\u548c\u4e24\u4e2a\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b PHP \u4ee3\u7801\uff1a &lt;?php \/\/ \u521b\u5efa\u547d\u4ee4\u5b57\u7b26\u4e32 $command = &#8216;python3 my_python_script.py &#8216; [&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-47886","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47886","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=47886"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47886\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}