Why does PHP automatically add '1' when I execute the command?
P粉512729862
P粉512729862 2023-09-17 13:04:54

I have a Python file that calls an API and gets some information, which I then need to use in a PHP file. But when running PHP, I get the error "'1' is not recognized as an internal or external command, operable program or batch file." Is this related to my use of sys.argv in the Python file? Specifically:

id_num = sys.argv[1]

The PHP code I am testing is as follows:

<?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);
?>

Print it out to make sure there is no problem with the command and the printout looks fine. Print output such as: python getData.py CRT67547

P粉512729862
P粉512729862

reply all(1)
P粉990568283

Edit: Re-read the question, revised for clarity

You may need to modify the shell_exec parameters in your PHP. In PHP, && is a AND logical operator, but I'm assuming you want it to be executed in the shell along with your two commands, like this:

$output = shell_exec($cd_command . '&&' . $command);

Or, to make your overall code cleaner:

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 the location you set, you can even do this:

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

You can simplify this to:

function getData($var_one)
{
    return shell_exec('python Location/getData.py ' . $var_one);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!