{"id":1112812,"date":"2025-01-08T17:41:59","date_gmt":"2025-01-08T09:41:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112812.html"},"modified":"2025-01-08T17:42:01","modified_gmt":"2025-01-08T09:42:01","slug":"%e5%a6%82%e4%bd%95%e7%94%a8php%e8%b0%83%e7%94%a8python%e8%84%9a%e6%9c%ac%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112812.html","title":{"rendered":"\u5982\u4f55\u7528php\u8c03\u7528python\u811a\u672c\u6587\u4ef6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074649\/c93dfd25-3db9-4ba8-b6e3-de9f2a498259.webp\" alt=\"\u5982\u4f55\u7528php\u8c03\u7528python\u811a\u672c\u6587\u4ef6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528PHP\u8c03\u7528Python\u811a\u672c\u6587\u4ef6\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u3001<code>shell_exec<\/code>\u51fd\u6570\u3001\u6216\u8005\u4f7f\u7528<code>proc_open<\/code>\u51fd\u6570\u3002<strong>\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u3001<code>shell_exec<\/code>\u51fd\u6570\u3001\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0PHP\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u5bf9\u5177\u4f53\u5b9e\u73b0\u8fdb\u884c\u8be6\u7ec6\u5c55\u5f00\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u8c03\u7528Python\u811a\u672c<\/p>\n<\/p>\n<p><p><code>exec<\/code>\u51fd\u6570\u662fPHP\u4e2d\u7528\u4e8e\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u8be5\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u811a\u672c\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$command = escapeshellcmd(&#39;python script.py&#39;);<\/p>\n<p>$output = exec($command);<\/p>\n<p>echo $output;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>escapeshellcmd<\/code>\u51fd\u6570\u7528\u4e8e\u8f6c\u4e49\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u653b\u51fb\u3002<code>exec<\/code>\u51fd\u6570\u6267\u884c\u4e86\u547d\u4ee4\u5e76\u5c06\u8f93\u51fa\u7ed3\u679c\u5b58\u50a8\u5728<code>$output<\/code>\u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>echo<\/code>\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>shell_exec<\/code>\u51fd\u6570\u8c03\u7528Python\u811a\u672c<\/p>\n<\/p>\n<p><p><code>shell_exec<\/code>\u51fd\u6570\u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u5728PHP\u4e2d\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u7684\u51fd\u6570\u3002\u4e0e<code>exec<\/code>\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c<code>shell_exec<\/code>\u51fd\u6570\u8fd4\u56de\u6574\u4e2a\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u800c\u4e0d\u662f\u6700\u540e\u4e00\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$output = shell_exec(&#39;python script.py&#39;);<\/p>\n<p>echo $output;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>shell_exec<\/code>\u51fd\u6570\u6267\u884c\u4e86\u547d\u4ee4\u5e76\u5c06\u8f93\u51fa\u7ed3\u679c\u5b58\u50a8\u5728<code>$output<\/code>\u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>echo<\/code>\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>proc_open<\/code>\u51fd\u6570\u8c03\u7528Python\u811a\u672c<\/p>\n<\/p>\n<p><p><code>proc_open<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728PHP\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u4e0e\u5176\u901a\u4fe1\u3002\u53ef\u4ee5\u901a\u8fc7\u8be5\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>    0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;),  \/\/ stdin is a pipe that the child will read from<\/p>\n<p>    1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),  \/\/ stdout is a pipe that the child will write to<\/p>\n<p>    2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;)   \/\/ stderr is a pipe that the child will write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(&#39;python script.py&#39;, $descriptorspec, $pipes);<\/p>\n<p>if (is_resource($process)) {<\/p>\n<p>    \/\/ Write to stdin<\/p>\n<p>    fwrite($pipes[0], &#39;input data&#39;);<\/p>\n<p>    fclose($pipes[0]);<\/p>\n<p>    \/\/ Read from stdout<\/p>\n<p>    $output = stream_get_contents($pipes[1]);<\/p>\n<p>    fclose($pipes[1]);<\/p>\n<p>    \/\/ Read from stderr<\/p>\n<p>    $error = stream_get_contents($pipes[2]);<\/p>\n<p>    fclose($pipes[2]);<\/p>\n<p>    \/\/ Close the process<\/p>\n<p>    $return_value = proc_close($process);<\/p>\n<p>    echo &quot;Output: $output\\n&quot;;<\/p>\n<p>    echo &quot;Error: $error\\n&quot;;<\/p>\n<p>    echo &quot;Return value: $return_value\\n&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>$descriptorspec<\/code>\u6570\u7ec4\u5b9a\u4e49\u4e86\u5b50\u8fdb\u7a0b\u7684\u4e09\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1a\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<code>proc_open<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fdb\u7a0b\u8d44\u6e90\u3002\u901a\u8fc7\u8be5\u8d44\u6e90\uff0c\u53ef\u4ee5\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>proc_close<\/code>\u51fd\u6570\u5173\u95ed\u5b50\u8fdb\u7a0b\uff0c\u5e76\u83b7\u53d6\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5411Python\u811a\u672c\u4f20\u9012\u53c2\u6570\uff0c\u5e76\u83b7\u53d6\u811a\u672c\u7684\u8fd4\u56de\u503c\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$arg1 = &#39;value1&#39;;<\/p>\n<p>$arg2 = &#39;value2&#39;;<\/p>\n<p>$command = escapeshellcmd(&quot;python script.py $arg1 $arg2&quot;);<\/p>\n<p>$output = shell_exec($command);<\/p>\n<p>echo $output;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5c06\u53c2\u6570\u6dfb\u52a0\u5230\u547d\u4ee4\u884c\u4e2d\uff0c\u53ef\u4ee5\u5411Python\u811a\u672c\u4f20\u9012\u53c2\u6570\u3002Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7<code>sys.argv<\/code>\u83b7\u53d6\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>arg1 = sys.argv[1]<\/p>\n<p>arg2 = sys.argv[2]<\/p>\n<p>print(f&#39;Argument 1: {arg1}&#39;)<\/p>\n<p>print(f&#39;Argument 2: {arg2}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u901a\u8fc7<code>sys.argv<\/code>\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Python\u811a\u672c\u5904\u7406\u8f93\u5165\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u548c\u6807\u51c6\u8f93\u51fa\u4e0ePython\u811a\u672c\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u901a\u8fc7<code>proc_open<\/code>\u51fd\u6570\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>    0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;),  \/\/ stdin is a pipe that the child will read from<\/p>\n<p>    1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),  \/\/ stdout is a pipe that the child will write to<\/p>\n<p>    2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;)   \/\/ stderr is a pipe that the child will write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(&#39;python script.py&#39;, $descriptorspec, $pipes);<\/p>\n<p>if (is_resource($process)) {<\/p>\n<p>    \/\/ Write to stdin<\/p>\n<p>    fwrite($pipes[0], &#39;input data&#39;);<\/p>\n<p>    fclose($pipes[0]);<\/p>\n<p>    \/\/ Read from stdout<\/p>\n<p>    $output = stream_get_contents($pipes[1]);<\/p>\n<p>    fclose($pipes[1]);<\/p>\n<p>    \/\/ Read from stderr<\/p>\n<p>    $error = stream_get_contents($pipes[2]);<\/p>\n<p>    fclose($pipes[2]);<\/p>\n<p>    \/\/ Close the process<\/p>\n<p>    $return_value = proc_close($process);<\/p>\n<p>    echo &quot;Output: $output\\n&quot;;<\/p>\n<p>    echo &quot;Error: $error\\n&quot;;<\/p>\n<p>    echo &quot;Return value: $return_value\\n&quot;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5411\u6807\u51c6\u8f93\u5165\u5199\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4f20\u9012\u7ed9Python\u811a\u672c\u3002Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7<code>sys.stdin<\/code>\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>input_data = sys.stdin.read()<\/p>\n<p>print(f&#39;Input data: {input_data}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u901a\u8fc7<code>sys.stdin.read<\/code>\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u7684\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406Python\u811a\u672c\u7684\u8f93\u51fa\u548c\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406Python\u811a\u672c\u7684\u8f93\u51fa\u548c\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7<code>proc_open<\/code>\u51fd\u6570\u83b7\u53d6\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>    0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;),  \/\/ stdin is a pipe that the child will read from<\/p>\n<p>    1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),  \/\/ stdout is a pipe that the child will write to<\/p>\n<p>    2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;)   \/\/ stderr is a pipe that the child will write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(&#39;python script.py&#39;, $descriptorspec, $pipes);<\/p>\n<p>if (is_resource($process)) {<\/p>\n<p>    \/\/ Write to stdin<\/p>\n<p>    fwrite($pipes[0], &#39;input data&#39;);<\/p>\n<p>    fclose($pipes[0]);<\/p>\n<p>    \/\/ Read from stdout<\/p>\n<p>    $output = stream_get_contents($pipes[1]);<\/p>\n<p>    fclose($pipes[1]);<\/p>\n<p>    \/\/ Read from stderr<\/p>\n<p>    $error = stream_get_contents($pipes[2]);<\/p>\n<p>    fclose($pipes[2]);<\/p>\n<p>    \/\/ Close the process<\/p>\n<p>    $return_value = proc_close($process);<\/p>\n<p>    \/\/ Process the output and error<\/p>\n<p>    if ($return_value === 0) {<\/p>\n<p>        echo &quot;Output: $output\\n&quot;;<\/p>\n<p>    } else {<\/p>\n<p>        echo &quot;Error: $error\\n&quot;;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u8bfb\u53d6\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c\u53ef\u4ee5\u83b7\u53d6Python\u811a\u672c\u7684\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002\u6839\u636e\u8fd4\u56de\u503c\uff0c\u5224\u65ad\u811a\u672c\u662f\u5426\u6267\u884c\u6210\u529f\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728PHP\u548cPython\u4e4b\u95f4\u4f20\u9012\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$data = array(&#39;key1&#39; =&gt; &#39;value1&#39;, &#39;key2&#39; =&gt; &#39;value2&#39;);<\/p>\n<p>$json_data = json_encode($data);<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>    0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;),  \/\/ stdin is a pipe that the child will read from<\/p>\n<p>    1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),  \/\/ stdout is a pipe that the child will write to<\/p>\n<p>    2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;)   \/\/ stderr is a pipe that the child will write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(&#39;python script.py&#39;, $descriptorspec, $pipes);<\/p>\n<p>if (is_resource($process)) {<\/p>\n<p>    \/\/ Write to stdin<\/p>\n<p>    fwrite($pipes[0], $json_data);<\/p>\n<p>    fclose($pipes[0]);<\/p>\n<p>    \/\/ Read from stdout<\/p>\n<p>    $output = stream_get_contents($pipes[1]);<\/p>\n<p>    fclose($pipes[1]);<\/p>\n<p>    \/\/ Read from stderr<\/p>\n<p>    $error = stream_get_contents($pipes[2]);<\/p>\n<p>    fclose($pipes[2]);<\/p>\n<p>    \/\/ Close the process<\/p>\n<p>    $return_value = proc_close($process);<\/p>\n<p>    \/\/ Process the output and error<\/p>\n<p>    if ($return_value === 0) {<\/p>\n<p>        $response_data = json_decode($output, true);<\/p>\n<p>        print_r($response_data);<\/p>\n<p>    } else {<\/p>\n<p>        echo &quot;Error: $error\\n&quot;;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>json_encode<\/code>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u4f20\u9012\u7ed9Python\u811a\u672c\u3002Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7<code>sys.stdin<\/code>\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>json.loads<\/code>\u51fd\u6570\u89e3\u6790JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import json<\/p>\n<p>input_data = sys.stdin.read()<\/p>\n<p>data = json.loads(input_data)<\/p>\n<p>print(json.dumps(data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u901a\u8fc7<code>sys.stdin.read<\/code>\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>json.loads<\/code>\u51fd\u6570\u89e3\u6790JSON\u6570\u636e\u3002\u901a\u8fc7<code>json.dumps<\/code>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406\u4e0d\u540c\u7684Python\u7248\u672c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u540c\u7684Python\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u6765\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$python_path = &#39;\/usr\/bin\/python3&#39;;  \/\/ Specify the path to the Python interpreter<\/p>\n<p>$command = escapeshellcmd(&quot;$python_path script.py&quot;);<\/p>\n<p>$output = shell_exec($command);<\/p>\n<p>echo $output;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6307\u5b9aPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7<code>which python<\/code>\u547d\u4ee4\u627e\u5230Python\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u8c03\u7528\u3002\u53ef\u4ee5\u4f7f\u7528PHP\u7684<code>pcntl_fork<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4f7f\u7528Python\u7684<code>asyncio<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pid = pcntl_fork();<\/p>\n<p>if ($pid == -1) {<\/p>\n<p>    \/\/ Fork f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led<\/p>\n<p>    die(&#39;could not fork&#39;);<\/p>\n<p>} else if ($pid) {<\/p>\n<p>    \/\/ Parent process<\/p>\n<p>    echo &quot;Parent process\\n&quot;;<\/p>\n<p>    pcntl_wait($status);  \/\/ Wait for child process to finish<\/p>\n<p>} else {<\/p>\n<p>    \/\/ Child process<\/p>\n<p>    $command = escapeshellcmd(&#39;python script.py&#39;);<\/p>\n<p>    $output = shell_exec($command);<\/p>\n<p>    echo $output;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>pcntl_fork<\/code>\u51fd\u6570\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528Python\u811a\u672c\u3002\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def main():<\/p>\n<p>    print(&#39;Hello, world!&#39;)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u3001<code>shell_exec<\/code>\u51fd\u6570\u3001<code>proc_open<\/code>\u51fd\u6570\uff0c\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c\uff0c\u5904\u7406\u8f93\u5165\u8f93\u51fa\uff0c\u5904\u7406\u8f93\u51fa\u548c\u9519\u8bef\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u5904\u7406\u4e0d\u540c\u7684Python\u7248\u672c\uff0c\u5904\u7406\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u8c03\u7528\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u5b9e\u73b0PHP\u8c03\u7528Python\u811a\u672c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c<strong>\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u3001<code>shell_exec<\/code>\u51fd\u6570\u3001<code>proc_open<\/code>\u51fd\u6570\uff0c\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c\uff0c\u5904\u7406\u8f93\u5165\u8f93\u51fa\uff0c\u5904\u7406\u8f93\u51fa\u548c\u9519\u8bef\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u5904\u7406\u4e0d\u540c\u7684Python\u7248\u672c\uff0c\u5904\u7406\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u8c03\u7528<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u5b9e\u73b0PHP\u4e0ePython\u7684\u96c6\u6210\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728PHP\u4e2d\u6267\u884cPython\u811a\u672c\u5e76\u83b7\u53d6\u8f93\u51fa\uff1f<\/strong><br \/>\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>exec()<\/code>\u3001<code>shell_exec()<\/code>\u6216<code>system()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u4f60\u6267\u884c\u5916\u90e8\u547d\u4ee4\u5e76\u6355\u83b7\u8f93\u51fa\u3002\u4f60\u9700\u8981\u786e\u4fddPython\u811a\u672c\u7684\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14\u6709\u6267\u884c\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\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>\u786e\u4fdd\u5c06<code>\/path\/to\/your_script.py<\/code>\u66ff\u6362\u4e3a\u4f60\u7684Python\u811a\u672c\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<p><strong>PHP\u548cPython\u4ea4\u4e92\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5b89\u5168\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u52a1\u5fc5\u6ce8\u610f\u8f93\u5165\u7684\u5b89\u5168\u6027\u3002\u786e\u4fdd\u4f20\u9012\u7ed9Python\u7684\u4efb\u4f55\u53c2\u6570\u90fd\u7ecf\u8fc7\u9a8c\u8bc1\u548c\u6e05\u7406\uff0c\u4ee5\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u7b49\u5b89\u5168\u6f0f\u6d1e\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528<code>escapeshellarg()<\/code>\u51fd\u6570\u6765\u5b89\u5168\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u4fdd\u62a4\u4f60\u7684\u7cfb\u7edf\u4e0d\u53d7\u6f5c\u5728\u653b\u51fb\u3002<\/p>\n<p><strong>\u5728PHP\u4e2d\u5982\u4f55\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff1f<\/strong><br \/>\u4f60\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u5f0f\u5c06\u6570\u636e\u4f20\u9012\u7ed9Python\u811a\u672c\u3002\u5728PHP\u4e2d\uff0c\u5c06\u53c2\u6570\u9644\u52a0\u5230\u6267\u884c\u547d\u4ee4\u7684\u5b57\u7b26\u4e32\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-php\">$arg1 = &#39;value1&#39;;\n$arg2 = &#39;value2&#39;;\n$output = shell_exec(&quot;python3 \/path\/to\/your_script.py $arg1 $arg2&quot;);\necho $output;\n<\/code><\/pre>\n<p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u6765\u83b7\u53d6\u8fd9\u4e9b\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\narg1 = sys.argv[1]\narg2 = sys.argv[2]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u4e24\u79cd\u8bed\u8a00\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528PHP\u8c03\u7528Python\u811a\u672c\u6587\u4ef6\u5185\u5bb9 \u8981\u5728PHP\u4e2d\u8c03\u7528Python\u811a\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a [&hellip;]","protected":false},"author":3,"featured_media":1112820,"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\/1112812"}],"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=1112812"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112812\/revisions"}],"predecessor-version":[{"id":1112823,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112812\/revisions\/1112823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112820"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}