我有一个Python文件,调用一个API并获取一些信息,然后我需要在一个PHP文件中使用这些信息。但是运行PHP时,我得到了“'1' is not recognized as an internal or external command, operable program or batch file.”的错误。这与我在Python文件中使用sys.argv有关吗? 具体来说:
id_num = sys.argv[1]
我正在测试的PHP代码如下:
<?php
function getData($var_one)
{
$cd_command = 'cd Location';
$command = 'python getData.py ' . $var_one;
print($command);
$output = shell_exec($cd_command && $command);
return $output;
}
$test_string = getData("CRT67547");
print($test_string);
?>
打印出来是为了确保命令没有问题,打印输出看起来没问题。
打印输出如:python getData.py CRT67547
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
编辑:重新阅读问题,为了清晰起见进行了修改
您可能需要修改您的PHP中的
shell_exec参数。在PHP中,&&是一个AND逻辑运算符,但我假设您希望它与您的两个命令一起在shell中执行,如下所示:或者,为了使您的整体代码更简洁:
function getData($var_one) { $command = 'cd Location && python getData.py ' . $var_one; $output = shell_exec($command); return $output; }然后您的shell应该运行
cd Location && python getData.py CRT67547。根据您设置的位置,您甚至可以这样做:
function getData($var_one) { $command = 'python Location/getData.py ' . $var_one; $output = shell_exec($command); return $output; }您可以将其简化为:
function getData($var_one) { return shell_exec('python Location/getData.py ' . $var_one); }