如何從 PHP 內呼叫 Python 檔案?


要從 PHP 檔案中呼叫 Python 檔案,需要使用 shell_exec 函式進行呼叫。

例如

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

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

#!/usr/bin/env python

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

例如

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

更新日期: 13-12-2019

1 萬餘次瀏覽

開啟你的 職業生涯

完成課程並取得認證

開始
廣告
© . All rights reserved.