首頁  >  文章  >  後端開發  >  如何從PHP呼叫Python檔?

如何從PHP呼叫Python檔?

WBOY
WBOY轉載
2023-09-24 15:29:021056瀏覽

如何從PHP呼叫Python檔?

要從 PHP 文件呼叫 Python 文件,您需要使用 shell_exec 函數來呼叫它。

例如

<?php
    $command = escapeshellcmd(&#39;/usr/custom/test.py&#39;);
    $output = shell_exec($command);
    echo $output;
?>

這將呼叫腳本。但在頂部的腳本中,您還需要指定解釋器。因此,在您的 py 檔案中,在頂部添加以下行:

#!/usr/bin/env python

或者,您也可以在執行命令時提供解釋器。

例如

<?php
    $command = escapeshellcmd(&#39;python3 /usr/custom/test.py&#39;);
    $output = shell_exec($command);
    echo $output;
?>
#

以上是如何從PHP呼叫Python檔?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除