当您尝试在PHP中运行命令时,为什么会在命令中添加’1’呢?

huangapple go评论103阅读模式
英文:

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:

&lt;?php
function getData($var_one)
{
    $cd_command = &#39;cd Location&#39;;
    $command = &#39;python getData.py &#39; . $var_one;
    print($command);
    $output = shell_exec($cd_command &amp;&amp; $command);
    return $output;
}

$test_string = getData(&quot;CRT67547&quot;);
print($test_string);
?&gt;

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参数。&amp;&amp;是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. &amp;&amp; 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 . &#39;&amp;&amp;&#39; . $command);

Or, to make your overall code more concise:

function getData($var_one)
{
    $command = &#39;cd Location &amp;&amp; python getData.py &#39; . $var_one;
    $output = shell_exec($command);
    return $output;
}

Your shell should then run cd Location &amp;&amp; python getData.py CRT67547.

Depending on how you set your Location, you could even do:

function getData($var_one)
{
    $command = &#39;python Location/getData.py &#39; . $var_one;
    $output = shell_exec($command);
    return $output;
}

which you can simplify down to:

function getData($var_one)
{
    return shell_exec(&#39;python Location/getData.py &#39; . $var_one);
}

huangapple
  • 本文由 发表于 2023年8月9日 01:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定