繁体   English   中英

无法从PHP shell_exec执行Python脚本

[英]Unable to execute Python script from PHP shell_exec

我正在尝试从PHP shell_exec函数执行python(Python 3)脚本,但是它似乎没有用,我正在Windows系统上尝试它,但是它将在生产中的CentOS 7中使用。

在下面的代码片段中,PHP运行时没有任何反应。 但是我使用$response = shell_exec("php -v"); 这行显示的是PHP版本。 我也尝试过直接在命令行上运行python脚本,它运行正常且没有错误。

    $command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
    $response = shell_exec($command);
    //$response = shell_exec("php -v");
    echo $response;

命令行

"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1

更新#1

我已经通过python运行了python脚本,但是一旦它到达driver = webdriver.PhantomJS() ,python脚本就会崩溃, from selenium import webdriver的python脚本中存在from selenium import webdriver并且一旦通过命令行,但是一旦从PHP调用python脚本,似乎就无法使用。

这是一个示例脚本,当从PHP脚本调用时,该脚本打印出test 1但不输出test 2

from selenium import webdriver

print("test 1")

driver = webdriver.PhantomJS()

print("test 2")

driver.quit()

更新#2

看起来这是路径问题,在python脚本中创建了一个使用相对路径的文件,一旦我将路径更改为完整路径,脚本就可以在命令行中以及在通过PHP脚本。

您需要转义命令字符串。

尝试使用escapeshellarg函数。

$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;

http://php.net/manual/pt_BR/function.escapeshellarg.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM