英文:
Why is '1' being added onto my command when I try to run it in PHP
问题
我有一个调用API并获取一些信息的Python文件,然后我需要在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
。
英文:
I have a python file that calls an api and gets some information back that I then need to use in a PHP file. But running the php gives me back "'1' is not recognized as an internal or external command, operable program or batch file." Is this related to me using sys.argv in the python file?
Specifically:
id_num = sys.argv[1]
My php I am testing looks like this:
<?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);
?>
The print is there to make sure nothing was wrong in the command and that print output looked fine.
Print output looked like python getData.py CRT67547
答案1
得分: 2
你可能需要修改你的PHP中的shell_exec
参数。&&
是PHP中的AND
逻辑运算符,但我猜你想要它与你的两个命令一起在shell中执行,像这样:
$output = shell_exec($cd_command . '&&' . $command);
或者,为了使你的整体代码更简洁:
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
。
根据你设置的Location,你甚至可以这样做:
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);
}
英文:
EDIT: reread the question, modified for clarity
You may want to modify the shell_exec
parameter in your PHP. &&
is an AND
logical operator in PHP, but I assume you want it to be executed in your shell along with your two commands like so:
$output = shell_exec($cd_command . '&&' . $command);
Or, to make your overall code more concise:
function getData($var_one)
{
$command = 'cd Location && python getData.py ' . $var_one;
$output = shell_exec($command);
return $output;
}
Your shell should then run cd Location && python getData.py CRT67547
.
Depending on how you set your Location, you could even do:
function getData($var_one)
{
$command = 'python Location/getData.py ' . $var_one;
$output = shell_exec($command);
return $output;
}
which you can simplify down to:
function getData($var_one)
{
return shell_exec('python Location/getData.py ' . $var_one);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论