{"id":1006296,"date":"2024-12-27T10:43:08","date_gmt":"2024-12-27T02:43:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006296.html"},"modified":"2024-12-27T10:43:11","modified_gmt":"2024-12-27T02:43:11","slug":"python%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5%e5%88%b0php","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1006296.html","title":{"rendered":"python\u5982\u4f55\u5d4c\u5165\u5230php"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082454\/77c69fd0-ed88-46a9-aaf4-124f2720d181.webp\" alt=\"python\u5982\u4f55\u5d4c\u5165\u5230php\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u5d4c\u5165\u5230PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528Python\u811a\u672c\u3001\u4f7f\u7528web\u670d\u52a1\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u3001\u4f7f\u7528PHP\u6269\u5c55\u6a21\u5757\u4e0ePython\u901a\u4fe1\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210Python\u4e0ePHP\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528Python\u811a\u672c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u76f4\u63a5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><p>\u5728PHP\u4e2d\u4f7f\u7528<code>exec()<\/code>\u3001<code>shell_exec()<\/code>\u6216<code>system()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5305\u62ec\u8c03\u7528Python\u811a\u672c\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6b63\u5e38\u6267\u884cPython\u811a\u672c\u3002\u63a5\u4e0b\u6765\uff0c\u5728PHP\u4ee3\u7801\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c<code>example.py<\/code>\uff0c\u5b9e\u73b0\u4e00\u4e9b\u8ba1\u7b97\u6216\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u7136\u540e\u5728PHP\u4e2d\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u8c03\u7528\u8be5\u811a\u672c\uff0c\u5e76\u901a\u8fc7\u6355\u83b7\u5176\u8f93\u51fa\u7ed3\u679c\u6765\u5b9e\u73b0\u6570\u636e\u7684\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u7ec6\u8282\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u5b9a\u4e49Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>$pythonScriptPath = &#39;\/path\/to\/example.py&#39;;<\/p>\n<p>\/\/ \u4f7f\u7528exec()\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>$output = [];<\/p>\n<p>$return_var = 0;<\/p>\n<p>exec(&quot;python3 $pythonScriptPath&quot;, $output, $return_var);<\/p>\n<p>\/\/ \u8f93\u51faPython\u811a\u672c\u7684\u8fd4\u56de\u7ed3\u679c<\/p>\n<p>if ($return_var === 0) {<\/p>\n<p>    echo &quot;Python script output: &quot; . implode(&quot;\\n&quot;, $output);<\/p>\n<p>} else {<\/p>\n<p>    echo &quot;Error executing Python script.&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528Python\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528\u662f\u5c06Python\u5d4c\u5165\u5230PHP\u4e2d\u7684\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7PHP\u7684\u5185\u7f6e\u51fd\u6570\u5982<code>exec()<\/code>\u3001<code>shell_exec()<\/code>\u6216<code>system()<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b9e\u73b0\u7b80\u5355\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u914d\u7f6e\u6216\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>exec()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>exec()<\/code>\u51fd\u6570\u7528\u4e8e\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\uff0c\u5e76\u5c06\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u8be5\u51fd\u6570\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u5176\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;example.py&#39;;<\/p>\n<p>$output = [];<\/p>\n<p>$return_var = 0;<\/p>\n<p>\/\/ \u8c03\u7528Python\u811a\u672c<\/p>\n<p>exec(&quot;python3 $pythonScript&quot;, $output, $return_var);<\/p>\n<p>\/\/ \u68c0\u67e5\u8fd4\u56de\u72b6\u6001\u5e76\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>if ($return_var === 0) {<\/p>\n<p>    echo &quot;Output: &quot; . implode(&quot;\\n&quot;, $output);<\/p>\n<p>} else {<\/p>\n<p>    echo &quot;Error executing Python script.&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>shell_exec()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>shell_exec()<\/code>\u51fd\u6570\u4e0e<code>exec()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5176\u8fd4\u56de\u503c\u662f\u5916\u90e8\u7a0b\u5e8f\u6267\u884c\u540e\u7684\u5b8c\u6574\u8f93\u51fa\uff08\u5b57\u7b26\u4e32\u5f62\u5f0f\uff09\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;example.py&#39;;<\/p>\n<p>\/\/ \u8c03\u7528Python\u811a\u672c\u5e76\u83b7\u53d6\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>$output = shell_exec(&quot;python3 $pythonScript&quot;);<\/p>\n<p>\/\/ \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>echo &quot;Output: &quot; . $output;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528<code>system()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>system()<\/code>\u51fd\u6570\u4e0e<code>exec()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u4f1a\u76f4\u63a5\u8f93\u51fa\u5916\u90e8\u7a0b\u5e8f\u7684\u7ed3\u679c\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u5e76\u8fd4\u56de\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u884c\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pythonScript = &#39;example.py&#39;;<\/p>\n<p>\/\/ \u8c03\u7528Python\u811a\u672c<\/p>\n<p>system(&quot;python3 $pythonScript&quot;, $return_var);<\/p>\n<p>\/\/ \u68c0\u67e5\u8fd4\u56de\u72b6\u6001<\/p>\n<p>if ($return_var !== 0) {<\/p>\n<p>    echo &quot;Error executing Python script.&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528web\u670d\u52a1\u8fdb\u884c\u6570\u636e\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5c06Python\u5d4c\u5165\u5230PHP\u4e2d\u7684\u65b9\u6cd5\u662f\u901a\u8fc7web\u670d\u52a1\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u5e73\u53f0\u6216\u4e0d\u540c\u8bed\u8a00\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u573a\u666f\u3002\u901a\u8fc7\u6784\u5efaRESTful API\uff0c\u53ef\u4ee5\u5b9e\u73b0PHP\u4e0ePython\u7684\u65e0\u7f1d\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u6784\u5efaPython web\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49Python\u6846\u67b6\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684web\u670d\u52a1\uff0c\u63d0\u4f9bRESTful API\u63a5\u53e3\u4f9bPHP\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Flask\u6784\u5efa\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, jsonify, request<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process_data():<\/p>\n<p>    # \u83b7\u53d6\u8bf7\u6c42\u4e2d\u7684\u6570\u636e<\/p>\n<p>    data = request.json<\/p>\n<p>    # \u8fdb\u884c\u6570\u636e\u5904\u7406<\/p>\n<p>    result = {&#39;processed_data&#39;: data[&#39;input&#39;] * 2}<\/p>\n<p>    return jsonify(result)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(port=5000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5728PHP\u4e2d\u8c03\u7528Python API<\/h4>\n<\/p>\n<p><p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528cURL\u6216file_get_contents()\u51fd\u6570\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u8c03\u7528Python web\u670d\u52a1\u63d0\u4f9b\u7684API\u63a5\u53e3\uff0c\u5e76\u83b7\u53d6\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u5b9a\u4e49API\u7684URL<\/p>\n<p>$url = &#39;http:\/\/localhost:5000\/process&#39;;<\/p>\n<p>\/\/ \u53d1\u9001POST\u8bf7\u6c42<\/p>\n<p>$data = [&#39;input&#39; =&gt; 42];<\/p>\n<p>$options = [<\/p>\n<p>    &#39;http&#39; =&gt; [<\/p>\n<p>        &#39;header&#39;  =&gt; &quot;Content-type: application\/json\\r\\n&quot;,<\/p>\n<p>        &#39;method&#39;  =&gt; &#39;POST&#39;,<\/p>\n<p>        &#39;content&#39; =&gt; json_encode($data),<\/p>\n<p>    ],<\/p>\n<p>];<\/p>\n<p>$context  = stream_context_create($options);<\/p>\n<p>$result = file_get_contents($url, false, $context);<\/p>\n<p>\/\/ \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>echo &quot;Processed data: &quot; . $result;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PHP\u6269\u5c55\u6a21\u5757\u4e0ePython\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>PHP\u6269\u5c55\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u5176\u4ed6\u8bed\u8a00\u901a\u4fe1\u7684\u529f\u80fd\uff0c\u4f8b\u5982PHP-FFI\uff08Foreign Function Interface\uff09\u6216Swig\uff08Simplified Wrapper and Interface Generator\uff09\u3002\u901a\u8fc7\u8fd9\u4e9b\u6269\u5c55\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0PHP\u4e0ePython\u4e4b\u95f4\u7684\u76f4\u63a5\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528PHP-FFI<\/h4>\n<\/p>\n<p><p>PHP-FFI\u662f\u4e00\u4e2a\u5141\u8bb8PHP\u8c03\u7528C\u8bed\u8a00\u51fd\u6570\u7684\u529f\u80fd\u6269\u5c55\u3002\u901a\u8fc7\u5c06Python\u7684C\u63a5\u53e3\u66b4\u9732\u7ed9PHP\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u7f16\u5199Python\u7684C\u6269\u5c55\u6a21\u5757\uff0c\u5e76\u7f16\u8bd1\u751f\u6210\u5171\u4eab\u5e93\u6587\u4ef6\uff08.so\u6216.dll\uff09\u3002\u7136\u540e\u5728PHP\u4e2d\u4f7f\u7528FFI\u52a0\u8f7d\u8be5\u5171\u4eab\u5e93\uff0c\u8c03\u7528Python\u7684C\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u52a0\u8f7dPython\u7684C\u5171\u4eab\u5e93<\/p>\n<p>$ffi = FFI::cdef(<\/p>\n<p>    &quot;int add(int a, int b);&quot;, <\/p>\n<p>    &quot;libexample.so&quot;<\/p>\n<p>);<\/p>\n<p>\/\/ \u8c03\u7528Python\u7684C\u51fd\u6570<\/p>\n<p>$result = $ffi-&gt;add(3, 4);<\/p>\n<p>\/\/ \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>echo &quot;Result: &quot; . $result;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Swig<\/h4>\n<\/p>\n<p><p>Swig\u662f\u4e00\u4e2a\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u751f\u6210PHP\u4e0ePython\u7684\u63a5\u53e3\u4ee3\u7801\u3002\u901a\u8fc7Swig\uff0c\u53ef\u4ee5\u5c06Python\u7684C\u63a5\u53e3\u66b4\u9732\u7ed9PHP\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u7f16\u5199Swig\u63a5\u53e3\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Swig\u751f\u6210PHP\u6269\u5c55\u6a21\u5757\u4ee3\u7801\u3002\u7136\u540e\u7f16\u8bd1\u751f\u6210PHP\u6269\u5c55\u6a21\u5757\uff0c\u5e76\u5728PHP\u4e2d\u52a0\u8f7d\u8be5\u6a21\u5757\uff0c\u8c03\u7528Python\u7684C\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>\/\/ \u52a0\u8f7dSwig\u751f\u6210\u7684PHP\u6269\u5c55\u6a21\u5757<\/p>\n<p>dl(&#39;example.so&#39;);<\/p>\n<p>\/\/ \u8c03\u7528Python\u7684C\u51fd\u6570<\/p>\n<p>$result = add(3, 4);<\/p>\n<p>\/\/ \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>echo &quot;Result: &quot; . $result;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210Python\u4e0ePHP<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316PHP\u4e0ePython\u4e4b\u95f4\u7684\u96c6\u6210\u8fc7\u7a0b\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u63a5\u53e3\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u548c\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528ZeroMQ<\/h4>\n<\/p>\n<p><p>ZeroMQ\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u5f02\u6b65\u6d88\u606f\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8ePHP\u4e0ePython\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u901a\u8fc7ZeroMQ\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u9ad8\u6548\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728PHP\u548cPython\u4e2d\u5b89\u88c5ZeroMQ\u5e93\uff0c\u7136\u540e\u7f16\u5199PHP\u548cPython\u7684ZeroMQ\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">\/\/ PHP ZeroMQ\u5ba2\u6237\u7aef\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>&lt;?php<\/p>\n<p>$context = new ZMQContext();<\/p>\n<p>$socket = $context-&gt;getSocket(ZMQ::SOCKET_REQ);<\/p>\n<p>$socket-&gt;connect(&quot;tcp:\/\/localhost:5555&quot;);<\/p>\n<p>\/\/ \u53d1\u9001\u8bf7\u6c42<\/p>\n<p>$socket-&gt;send(&quot;Hello&quot;);<\/p>\n<p>\/\/ \u63a5\u6536\u54cd\u5e94<\/p>\n<p>$response = $socket-&gt;recv();<\/p>\n<p>echo &quot;Received: &quot; . $response;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># Python ZeroMQ\u670d\u52a1\u5668\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>import zmq<\/p>\n<p>context = zmq.Context()<\/p>\n<p>socket = context.socket(zmq.REP)<\/p>\n<p>socket.bind(&quot;tcp:\/\/*:5555&quot;)<\/p>\n<p>while True:<\/p>\n<p>    # \u63a5\u6536\u8bf7\u6c42<\/p>\n<p>    message = socket.recv()<\/p>\n<p>    print(f&quot;Received request: {message}&quot;)<\/p>\n<p>    # \u53d1\u9001\u54cd\u5e94<\/p>\n<p>    socket.send(b&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528RabbitMQ<\/h4>\n<\/p>\n<p><p>RabbitMQ\u662f\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u53ef\u4ee5\u7528\u4e8ePHP\u4e0ePython\u4e4b\u95f4\u7684\u5f02\u6b65\u901a\u4fe1\u3002\u901a\u8fc7RabbitMQ\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u53ef\u9760\u6d88\u606f\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728PHP\u548cPython\u4e2d\u5b89\u88c5RabbitMQ\u5ba2\u6237\u7aef\u5e93\uff0c\u7136\u540e\u7f16\u5199PHP\u548cPython\u7684RabbitMQ\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c\u5b9e\u73b0\u6d88\u606f\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">\/\/ PHP RabbitMQ\u5ba2\u6237\u7aef\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>&lt;?php<\/p>\n<p>require_once __DIR__ . &#39;\/vendor\/autoload.php&#39;;<\/p>\n<p>use PhpAmqpLib\\Connection\\AMQPStreamConnection;<\/p>\n<p>use PhpAmqpLib\\Message\\AMQPMessage;<\/p>\n<p>$connection = new AMQPStreamConnection(&#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;hello&#39;, false, false, false, false);<\/p>\n<p>$msg = new AMQPMessage(&#39;Hello World!&#39;);<\/p>\n<p>$channel-&gt;basic_publish($msg, &#39;&#39;, &#39;hello&#39;);<\/p>\n<p>echo &quot; [x] Sent &#39;Hello World!&#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><pre><code class=\"language-python\"># Python RabbitMQ\u6d88\u8d39\u8005\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>import pika<\/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;hello&#39;)<\/p>\n<p>def callback(ch, method, properties, body):<\/p>\n<p>    print(f&quot;Received {body}&quot;)<\/p>\n<p>channel.basic_consume(queue=&#39;hello&#39;, on_message_callback=callback, auto_ack=True)<\/p>\n<p>print(&#39;Waiting for messages. To exit press CTRL+C&#39;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u5c06Python\u5d4c\u5165\u5230PHP\u4e2d\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u96c6\u6210\u4e0e\u901a\u4fe1\u3002\u8fd9\u79cd\u96c6\u6210\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u8fd8\u80fd\u591f\u5728PHP\u73af\u5883\u4e2d\u5b9e\u73b0\u7075\u6d3b\u7684\u6570\u636e\u5904\u7406\u548c\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>exec()<\/code>\u3001<code>shell_exec()<\/code>\u6216<code>system()<\/code>\u7b49\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u60a8\u5728PHP\u4ee3\u7801\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\u3002\u786e\u4fddPython\u5df2\u7ecf\u5b89\u88c5\uff0c\u5e76\u4e14\u811a\u672c\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-php\">$output = shell_exec(&#39;python3 \/path\/to\/your\/script.py&#39;);\necho $output;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728PHP\u4e2d\u83b7\u53d6Python\u811a\u672c\u7684\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728PHP\u548cPython\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728PHP\u548cPython\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002PHP\u53ef\u4ee5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u7ed9Python\u811a\u672c\u3002Python\u811a\u672c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-php\">$data = json_encode([&#39;key&#39; =&gt; &#39;value&#39;]);\n$output = shell_exec(&quot;python3 \/path\/to\/your\/script.py &#39;$data&#39;&quot;);\n<\/code><\/pre>\n<p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\u89e3\u6790\u4f20\u5165\u7684JSON\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406PHP\u548cPython\u4e4b\u95f4\u7684\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6267\u884c\u7ed3\u679c\u7684\u8fd4\u56de\u503c\u6765\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u3002\u901a\u5e38\uff0cPython\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u975e\u96f6\u503c\u6765\u6307\u793a\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u5728PHP\u4e2d\u68c0\u6d4b\u8fd9\u4e00\u70b9\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-php\">$return_var = 0;\n$output = [];\nexec(&#39;python3 \/path\/to\/your\/script.py&#39;, $output, $return_var);\nif ($return_var !== 0) {\n    echo &quot;Error occurred: &quot; . implode(&quot;\\n&quot;, $output);\n} else {\n    echo &quot;Script executed successfully: &quot; . implode(&quot;\\n&quot;, $output);\n}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406\u548c\u8c03\u8bd5\u8de8\u8bed\u8a00\u8c03\u7528\u65f6\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u5d4c\u5165\u5230PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u8c03\u7528Python\u811a\u672c\u3001\u4f7f\u7528web\u670d\u52a1\u8fdb\u884c\u6570\u636e\u4ea4\u4e92 [&hellip;]","protected":false},"author":3,"featured_media":1006303,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006296"}],"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=1006296"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006296\/revisions"}],"predecessor-version":[{"id":1006306,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006296\/revisions\/1006306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006303"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}