要在PHP中運行Python腳本,您可以使用exec()
或shell_exec()
函數
<?php
// 指定Python腳本的路徑
$python_script = '/path/to/your/python_script.py';
// 使用exec()函數運行Python腳本
$output = exec("python $python_script");
// 輸出結果
echo "<pre>$output</pre>";
?>
或者使用shell_exec()
函數:
<?php
// 指定Python腳本的路徑
$python_script = '/path/to/your/python_script.py';
// 使用shell_exec()函數運行Python腳本并獲取完整輸出
$output = shell_exec("python $python_script");
// 輸出結果
echo "<pre>$output</pre>";
?>
請注意,為了使這些示例正常工作,您的Web服務器需要具有執行Python腳本所需的權限。此外,確保將/path/to/your/python_script.py
替換為您的Python腳本的實際路徑。