我試圖從php
調用python腳本,一切都運行完美純php
這樣的:如何從php laravel運行python?
$data = $_POST['text'];
$result=exec('python_file.py ' . escapeshellarg(json_encode($data)));
$resultData = json_decode($result, true);
的問題是,當我使用同一線上laravel 3.2.13功能的一個我捆綁控制器,它不會執行命令並且不會出現錯誤!
有什麼幫助嗎?
這是我laravel功能看起來像:
public function action_test(){
$text="Test";
$result=exec('python_file.py ' . escapeshellarg(json_encode($text)));
$resultData = json_decode($result, true);
dd($result);
}
謝謝
$結果將只包含輸出的最後一行。如果你想完整的輸出你需要傳遞第二個參數,例如:'exec('python_file.py'。escapeshellarg(json_encode($ text)),$ result);' – 2014-09-06 11:18:52
好的,我做了,現在有問題了在路徑, 我所說的文件的完整路徑是這樣的: MyDrive:\ XAMPP \ htdocs中\ MyProject的\束\ myBundle \控制器\ python_file.py 它給這個錯誤: [鏈接] FileNotFoundError:'[錯誤2 ]沒有這樣的文件或目錄:'。\\ config.xml'' – Amr 2014-09-06 11:56:56
你可以做一個堅實的&使用shell_exec而不是exec。讓我們知道你是否得到完整的字符串輸出。 – 2017-01-11 12:15:27