如何從 PHP 檔案內呼叫 Python 檔案?


要在 PHP 檔案內呼叫 Python 檔案,您需要使用 shell_exec 函式對其進行呼叫。

例如

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

這將呼叫指令碼。但在您指令碼的頂部,還需要指定直譯器。因此,在您的 py 檔案中,在頂部新增以下行

#!/usr/bin/env python

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

例如

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

更新於: 2019 年 12 月 13 日

10K+ 檢視

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.