{"id":1175578,"date":"2025-01-15T17:33:56","date_gmt":"2025-01-15T09:33:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175578.html"},"modified":"2025-01-15T17:33:56","modified_gmt":"2025-01-15T09:33:56","slug":"%e5%a6%82%e4%bd%95%e7%94%a8php%e8%b0%83%e7%94%a8python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175578.html","title":{"rendered":"\u5982\u4f55\u7528php\u8c03\u7528python\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111000\/01c84306-b27a-4dfa-9c36-fb91968ce958.webp\" alt=\"\u5982\u4f55\u7528php\u8c03\u7528python\u811a\u672c\" \/><\/p>\n<p><p> <strong>\u8981\u7528PHP\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7exec()\u51fd\u6570\u3001shell_exec()\u51fd\u6570\u3001\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u3001\u6216\u901a\u8fc7Web\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3002<\/strong> \u5176\u4e2d\uff0cexec()\u51fd\u6570\u548cshell_exec()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528exec()\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><p>PHP\u7684exec()\u51fd\u6570\u5141\u8bb8\u60a8\u4ecePHP\u811a\u672c\u4e2d\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002\u8981\u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u60a8\u9700\u8981\u786e\u4fddPython\u5df2\u5b89\u88c5\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u5e76\u4e14Python\u811a\u672c\u5177\u6709\u9002\u5f53\u7684\u6267\u884c\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u5b9a\u4e49Python\u811a\u672c\u8def\u5f84<\/p>\n<p>$pythonScriptPath = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>\/\/ \u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u6355\u83b7\u8f93\u51fa<\/p>\n<p>$output = [];<\/p>\n<p>exec(&quot;python3 &quot; . $pythonScriptPath, $output);<\/p>\n<p>\/\/ \u8f93\u51faPython\u811a\u672c\u7684\u7ed3\u679c<\/p>\n<p>foreach ($output as $line) {<\/p>\n<p>    echo $line . &quot;&lt;br&gt;&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u5c06\u8f93\u51fa\u5b58\u50a8\u5728$output\u6570\u7ec4\u4e2d\u3002\u7136\u540e\uff0c\u901a\u8fc7foreach\u5faa\u73af\u9010\u884c\u8f93\u51faPython\u811a\u672c\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u786e\u4fddPython\u811a\u672c\u5177\u6709\u9002\u5f53\u7684\u6267\u884c\u6743\u9650<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u60a8\u9700\u8981\u786e\u4fddPython\u811a\u672c\u6587\u4ef6\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\uff08\u4f8b\u5982\uff0c\u901a\u8fc7chmod\u547d\u4ee4\u8bbe\u7f6e\u6743\u9650\uff09\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u9700\u8981\u786e\u4fddPHP\u811a\u672c\u6709\u8db3\u591f\u7684\u6743\u9650\u6765\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528exec()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>exec()\u51fd\u6570\u662fPHP\u4e2d\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">exec(string $command, array &amp;$output = null, int &amp;$result_code = null): string<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>$command<\/code>\uff1a\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<li><code>$output<\/code>\uff1a\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u547d\u4ee4\u8f93\u51fa\u7684\u6bcf\u4e00\u884c\u3002<\/li>\n<li><code>$result_code<\/code>\uff1a\u5b58\u50a8\u547d\u4ee4\u7684\u8fd4\u56de\u72b6\u6001\u7801\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$output = [];<\/p>\n<p>$result_code = 0;<\/p>\n<p>exec(&quot;python3 &quot; . $pythonScript, $output, $result_code);<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>foreach ($output as $line) {<\/p>\n<p>    echo $line . &quot;&lt;br&gt;&quot;;<\/p>\n<p>}<\/p>\n<p>echo &quot;Return Code: &quot; . $result_code;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u6355\u83b7\u8f93\u51fa\u548c\u8fd4\u56de\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u8f93\u5165\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5411Python\u811a\u672c\u4f20\u9012\u53c2\u6570\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u53c2\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$param1 = &#39;value1&#39;;<\/p>\n<p>$param2 = &#39;value2&#39;;<\/p>\n<p>$output = [];<\/p>\n<p>$result_code = 0;<\/p>\n<p>exec(&quot;python3 &quot; . $pythonScript . &quot; &quot; . $param1 . &quot; &quot; . $param2, $output, $result_code);<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>foreach ($output as $line) {<\/p>\n<p>    echo $line . &quot;&lt;br&gt;&quot;;<\/p>\n<p>}<\/p>\n<p>echo &quot;Return Code: &quot; . $result_code;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5411Python\u811a\u672c\u4f20\u9012\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u6355\u83b7\u4e86\u8f93\u51fa\u548c\u8fd4\u56de\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528shell_exec()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>shell_exec()\u51fd\u6570\u662fPHP\u4e2d\u53e6\u4e00\u4e2a\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u7684\u65b9\u6cd5\u3002\u4e0eexec()\u51fd\u6570\u4e0d\u540c\uff0cshell_exec()\u51fd\u6570\u8fd4\u56de\u6574\u4e2a\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u800c\u4e0d\u662f\u9010\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>shell_exec()\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">shell_exec(string $command): string<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$output = shell_exec(&quot;python3 &quot; . $pythonScript);<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>echo nl2br($output);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528shell_exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u8f93\u51fa\u6574\u4e2a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u8f93\u5165\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4e0eexec()\u51fd\u6570\u7c7b\u4f3c\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u53c2\u6570\u6765\u5411Python\u811a\u672c\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$param1 = &#39;value1&#39;;<\/p>\n<p>$param2 = &#39;value2&#39;;<\/p>\n<p>$output = shell_exec(&quot;python3 &quot; . $pythonScript . &quot; &quot; . $param1 . &quot; &quot; . $param2);<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>echo nl2br($output);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5411Python\u811a\u672c\u4f20\u9012\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8f93\u51fa\u4e86\u6574\u4e2a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528<\/h3>\n<\/p>\n<p><p>PHP\u8fd8\u63d0\u4f9b\u4e86system()\u51fd\u6570\u548cpassthru()\u51fd\u6570\u6765\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u3002\u5b83\u4eec\u7684\u7528\u6cd5\u7c7b\u4f3c\u4e8eexec()\u548cshell_exec()\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528system()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>system()\u51fd\u6570\u6267\u884c\u7ed9\u5b9a\u7684\u547d\u4ee4\uff0c\u5e76\u76f4\u63a5\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$param1 = &#39;value1&#39;;<\/p>\n<p>$param2 = &#39;value2&#39;;<\/p>\n<p>system(&quot;python3 &quot; . $pythonScript . &quot; &quot; . $param1 . &quot; &quot; . $param2);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528passthru()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>passthru()\u51fd\u6570\u4e0esystem()\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e13\u95e8\u7528\u4e8e\u8f93\u51fa\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$param1 = &#39;value1&#39;;<\/p>\n<p>$param2 = &#39;value2&#39;;<\/p>\n<p>passthru(&quot;python3 &quot; . $pythonScript . &quot; &quot; . $param1 . &quot; &quot; . $param2);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7Web\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5c06PHP\u548cPython\u811a\u672c\u96c6\u6210\u5728\u4e00\u8d77\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u901a\u8fc7Web\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPHP\u548cPython\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528PHP\u7684cURL\u5e93\u6216file_get_contents()\u51fd\u6570\u6765\u5411Python Web\u670d\u52a1\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonApiUrl = &#39;http:\/\/your-python-api-endpoint&#39;;<\/p>\n<p>$params = [<\/p>\n<p>    &#39;param1&#39; =&gt; &#39;value1&#39;,<\/p>\n<p>    &#39;param2&#39; =&gt; &#39;value2&#39;,<\/p>\n<p>];<\/p>\n<p>$ch = curl_init();<\/p>\n<p>curl_setopt($ch, CURLOPT_URL, $pythonApiUrl);<\/p>\n<p>curl_setopt($ch, CURLOPT_POST, 1);<\/p>\n<p>curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));<\/p>\n<p>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<\/p>\n<p>$response = curl_exec($ch);<\/p>\n<p>curl_close($ch);<\/p>\n<p>echo &quot;Response: &quot; . $response;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528cURL\u5e93\u5411Python Web\u670d\u52a1\u53d1\u9001POST\u8bf7\u6c42\uff0c\u5e76\u8f93\u51fa\u54cd\u5e94\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u6587\u4ef6\u4f5c\u4e3a\u4e2d\u4ecb<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u6587\u4ef6\u8fdb\u884c\u901a\u4fe1\u3002PHP\u811a\u672c\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0cPython\u811a\u672c\u8bfb\u53d6\u6587\u4ef6\u5e76\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u5199\u56de\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u5199\u5165\u6570\u636e\u5230\u6587\u4ef6<\/p>\n<p>$data = [<\/p>\n<p>    &#39;param1&#39; =&gt; &#39;value1&#39;,<\/p>\n<p>    &#39;param2&#39; =&gt; &#39;value2&#39;,<\/p>\n<p>];<\/p>\n<p>file_put_contents(&#39;\/path\/to\/input.json&#39;, json_encode($data));<\/p>\n<p>\/\/ \u8c03\u7528Python\u811a\u672c<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>exec(&quot;python3 &quot; . $pythonScript);<\/p>\n<p>\/\/ \u8bfb\u53d6Python\u811a\u672c\u7684\u7ed3\u679c<\/p>\n<p>$result = file_get_contents(&#39;\/path\/to\/output.json&#39;);<\/p>\n<p>echo &quot;Result: &quot; . $result;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6587\u4ef6\u4f5c\u4e3a\u4e2d\u4ecb\uff0cPHP\u811a\u672c\u5199\u5165\u6570\u636e\uff0cPython\u811a\u672c\u8bfb\u53d6\u5e76\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u5199\u56de\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u6d88\u606f\u961f\u5217\u8fdb\u884c\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u590d\u6742\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6d88\u606f\u961f\u5217\uff08\u4f8b\u5982RabbitMQ\u3001Redis\u3001Kafka\uff09\u8fdb\u884c\u901a\u4fe1\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528RabbitMQ<\/h4>\n<\/p>\n<p><p>RabbitMQ\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0PHP\u548cPython\u4e4b\u95f4\u7684\u5f02\u6b65\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5RabbitMQ\u5e76\u542f\u52a8\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5b89\u88c5PHP\u7684AMQP\u6269\u5c55\u548cPython\u7684pika\u5e93\u3002<\/p>\n<\/p>\n<p><p>PHP\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$connection = new AMQPConnection(&#39;localhost&#39;, 5672, &#39;guest&#39;, &#39;guest&#39;);<\/p>\n<p>$channel = $connection-&gt;channel();<\/p>\n<p>$channel-&gt;queue_declare(&#39;task_queue&#39;, false, true, false, false);<\/p>\n<p>$data = json_encode([&#39;param1&#39; =&gt; &#39;value1&#39;, &#39;param2&#39; =&gt; &#39;value2&#39;]);<\/p>\n<p>$msg = new AMQPMessage($data, [&#39;delivery_mode&#39; =&gt; 2]);<\/p>\n<p>$channel-&gt;basic_publish($msg, &#39;&#39;, &#39;task_queue&#39;);<\/p>\n<p>echo &quot; [x] Sent &#39;Task!&#39;\\n&quot;;<\/p>\n<p>$channel-&gt;close();<\/p>\n<p>$connection-&gt;close();<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>import json<\/p>\n<p>def callback(ch, method, properties, body):<\/p>\n<p>    data = json.loads(body)<\/p>\n<p>    print(f&quot;Received {data}&quot;)<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>    ch.basic_ack(delivery_tag=method.delivery_tag)<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;task_queue&#39;, durable=True)<\/p>\n<p>channel.basic_qos(prefetch_count=1)<\/p>\n<p>channel.basic_consume(queue=&#39;task_queue&#39;, on_message_callback=callback)<\/p>\n<p>print(&#39; [*] W<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ting for messages. To exit press CTRL+C&#39;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cPHP\u811a\u672c\u5c06\u4efb\u52a1\u53d1\u9001\u5230RabbitMQ\u961f\u5217\uff0cPython\u811a\u672c\u4ece\u961f\u5217\u4e2d\u8bfb\u53d6\u4efb\u52a1\u5e76\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u65f6\uff0c\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528try-catch\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528try-catch\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>try {<\/p>\n<p>    $pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>    $output = [];<\/p>\n<p>    $result_code = 0;<\/p>\n<p>    exec(&quot;python3 &quot; . $pythonScript, $output, $result_code);<\/p>\n<p>    if ($result_code !== 0) {<\/p>\n<p>        throw new Exception(&quot;Python script returned error code: &quot; . $result_code);<\/p>\n<p>    }<\/p>\n<p>    echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>    foreach ($output as $line) {<\/p>\n<p>        echo $line . &quot;&lt;br&gt;&quot;;<\/p>\n<p>    }<\/p>\n<p>} catch (Exception $e) {<\/p>\n<p>    echo &quot;Error: &quot; . $e-&gt;getMessage();<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528try-catch\u8bed\u53e5\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bb0\u5f55\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u8bb0\u5f55\u65e5\u5fd7\u662f\u8c03\u8bd5\u548c\u6392\u67e5\u95ee\u9898\u7684\u91cd\u8981\u624b\u6bb5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528PHP\u7684error_log()\u51fd\u6570\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>try {<\/p>\n<p>    $pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>    $output = [];<\/p>\n<p>    $result_code = 0;<\/p>\n<p>    exec(&quot;python3 &quot; . $pythonScript, $output, $result_code);<\/p>\n<p>    if ($result_code !== 0) {<\/p>\n<p>        throw new Exception(&quot;Python script returned error code: &quot; . $result_code);<\/p>\n<p>    }<\/p>\n<p>    echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>    foreach ($output as $line) {<\/p>\n<p>        echo $line . &quot;&lt;br&gt;&quot;;<\/p>\n<p>    }<\/p>\n<p>} catch (Exception $e) {<\/p>\n<p>    error_log(&quot;Error: &quot; . $e-&gt;getMessage());<\/p>\n<p>    echo &quot;An error occurred. Please check the log file for details.&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528error_log()\u51fd\u6570\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u6bcf\u6b21\u7cfb\u7edf\u8c03\u7528\u90fd\u4f1a\u6d88\u8017\u4e00\u5b9a\u7684\u8d44\u6e90\uff0c\u56e0\u6b64\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5c0f\u4efb\u52a1\u5408\u5e76\u4e3a\u4e00\u4e2a\u5927\u4efb\u52a1\uff0c\u4ee5\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5916\u90e8\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u53ef\u9884\u6d4b\u7684\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u6765\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5916\u90e8\u7a0b\u5e8f\u7684\u8f93\u51fa\u7f13\u5b58\u5230\u6587\u4ef6\u6216\u5185\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u4e0b\u6b21\u76f4\u63a5\u8bfb\u53d6\u7f13\u5b58\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$cacheFile = &#39;\/path\/to\/cache.json&#39;;<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>if (file_exists($cacheFile)) {<\/p>\n<p>    $output = file_get_contents($cacheFile);<\/p>\n<p>} else {<\/p>\n<p>    $output = shell_exec(&quot;python3 &quot; . $pythonScript);<\/p>\n<p>    file_put_contents($cacheFile, $output);<\/p>\n<p>}<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>echo nl2br($output);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6587\u4ef6\u7f13\u5b58\u6765\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5f02\u6b65\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5916\u90e8\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u5904\u7406\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d88\u606f\u961f\u5217\u5c06\u4efb\u52a1\u53d1\u9001\u5230\u540e\u53f0\u5904\u7406\uff0c\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b89\u5168\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u65f6\uff0c\u5b89\u5168\u6027\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b89\u5168\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u9a8c\u8bc1\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u5c06\u7528\u6237\u8f93\u5165\u4f20\u9012\u7ed9\u5916\u90e8\u7a0b\u5e8f\u4e4b\u524d\uff0c\u786e\u4fdd\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u548c\u8fc7\u6ee4\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$param1 = escapeshellarg($_GET[&#39;param1&#39;]);<\/p>\n<p>$param2 = escapeshellarg($_GET[&#39;param2&#39;]);<\/p>\n<p>$pythonScript = &#39;\/path\/to\/your\/script.py&#39;;<\/p>\n<p>$output = shell_exec(&quot;python3 &quot; . $pythonScript . &quot; &quot; . $param1 . &quot; &quot; . $param2);<\/p>\n<p>echo &quot;Output:&lt;br&gt;&quot;;<\/p>\n<p>echo nl2br($output);<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528escapeshellarg()\u51fd\u6570\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9650\u5236\u6267\u884c\u6743\u9650<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u53ea\u6709\u53ef\u4fe1\u7684\u7528\u6237\u548c\u811a\u672c\u5177\u6709\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u7684\u6743\u9650\u3002\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u64cd\u4f5c\u7cfb\u7edf\u6743\u9650\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5b89\u5168\u7684\u901a\u4fe1\u534f\u8bae<\/h4>\n<\/p>\n<p><p>\u5728\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5b89\u5168\u7684\u901a\u4fe1\u534f\u8bae\uff08\u4f8b\u5982HTTPS\uff09\u6765\u4fdd\u62a4\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>PHP\u8c03\u7528Python\u811a\u672c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528exec()\u51fd\u6570\u3001shell_exec()\u51fd\u6570\u3001\u7cfb\u7edf\u8c03\u7528\u3001\u901a\u8fc7Web\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3001\u901a\u8fc7\u6d88\u606f\u961f\u5217\u8fdb\u884c\u901a\u4fe1\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u4f18\u5316\u548c\u5b89\u5168\u63aa\u65bd\uff0c\u53ef\u4ee5\u786e\u4fddPHP\u8c03\u7528Python\u811a\u672c\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0PHP\u4e0ePython\u7684\u96c6\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728PHP\u4e2d\u5b9e\u73b0\u4e0ePython\u811a\u672c\u7684\u4ea4\u4e92\uff1f<\/strong><br \/>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4e0ePython\u811a\u672c\u8fdb\u884c\u4ea4\u4e92\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>exec()<\/code>\u3001<code>shell_exec()<\/code>\u6216<code>system()<\/code>\u51fd\u6570\u6765\u8fd0\u884cPython\u811a\u672c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u60a8\u5728PHP\u4ee3\u7801\u4e2d\u8c03\u7528\u547d\u4ee4\u884c\u547d\u4ee4\uff0c\u4ece\u800c\u6267\u884cPython\u811a\u672c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u786e\u4fddPython\u811a\u672c\u5177\u6709\u6267\u884c\u6743\u9650\uff0c\u5e76\u4e14\u5728PHP\u4e2d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u907f\u514d\u8def\u5f84\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u9700\u8981\u6ce8\u610fPHP\u7684\u6267\u884c\u73af\u5883\u4ee5\u53caPython\u7684\u73af\u5883\u914d\u7f6e\u3002\u786e\u4fddPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14\u6240\u9700\u7684\u5e93\u5df2\u7ecf\u5b89\u88c5\u3002\u5982\u679cPHP\u8fd0\u884c\u5728Web\u670d\u52a1\u5668\u4e0a\uff0c\u6743\u9650\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u9650\u5236\u811a\u672c\u7684\u6267\u884c\u3002\u56e0\u6b64\uff0c\u8c03\u8bd5\u65f6\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u6355\u6349\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u8003\u8651\u5230\u5b89\u5168\u6027\uff0c\u907f\u514d\u76f4\u63a5\u5c06\u7528\u6237\u8f93\u5165\u4f20\u9012\u7ed9Python\u811a\u672c\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u653b\u51fb\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406PHP\u4e0ePython\u811a\u672c\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\uff1f<\/strong><br \/>PHP\u4e0ePython\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u8fdb\u884c\u6570\u636e\u4f20\u9012\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json_encode()<\/code>\u5c06PHP\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9Python\u811a\u672c\u3002Python\u811a\u672c\u53ef\u4ee5\u4f7f\u7528<code>json.loads()<\/code>\u6765\u89e3\u6790\u8be5JSON\u6570\u636e\u3002\u53cd\u4e4b\uff0cPython\u811a\u672c\u7684\u8f93\u51fa\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>json_encode()<\/code>\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u65b9\u4fbf\u5728PHP\u4e2d\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u6570\u636e\u5728\u4e24\u8005\u4e4b\u95f4\u4f20\u9012\u65f6\u4fdd\u6301\u7ed3\u6784\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528PHP\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7exec()\u51fd\u6570\u3001shell_exec()\u51fd\u6570\u3001\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u3001\u6216\u901a\u8fc7W [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175578"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1175578"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175578\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}