PHP show return from python script - php

I am unable to get php to show full results of my python script, only the last basic print statement is printed. What am I doing wrong here? Why is the php not showing the output from the python script?
php
$result = exec('python list.py');
echo $result;
list.py
import subprocess
print "start"
a = subprocess.Popen(["ls", "-a"], stdout=subprocess.PIPE)
a.wait()
result_str = a.stdout.read()
print result_str
print "stop"
the command line output for that python script is as below
start
...filename
...filename
...etc...etc
stop
The php output from executing the python script is only
stop
Thank you

You need to pass $result as a param into exec() otherwise you only get the last statement.
exec('python list.py', $result);
print_r($result);
This will get an array of output, $result[0] will contain stop and $result[sizeof($result)-1] will contain stop and everything in between will contain the rest of your program output.

exec will always return the last line from the result of the command. So, if you need to get every line of output from the command executed. Then, you need to write as below :
$result = exec('python list.py', $ouput);
print_r($output)
As per exec documentation
string exec ( string $command [, array &$output [, int &$return_var ]] )
If the output argument is present, then the specified array will be
filled with every line of output from the command. Trailing
whitespace, such as \n, is not included in this array. Note that if
the array already contains some elements, exec() will append to the
end of the array. If you do not want the function to append elements,
call unset() on the array before passing it to exec().

Related

Store a value on PHP coming from a CLI Command Output (Windows)

I'm trying get a value (12345678) coming from a password vault CLI output and store this on a variable in PHP.
For example on PERL, I'm using this:
my $pass= qx("C:\\Program Files (x86)\\CyberArk\\ApplicationPasswordSdk\\CLIPasswordSDK.exe" GetPassword /p AppDescs.AppID=SOMEAPP /p Query="Safe=SAFE;Folder=Root;Object=DATABASEX" /o Password);
chomp($pass);
The value of the variable $pass is 12345678 but I can't reproduce this on PHP, anyone can help me with that?
In PHP you can use the exec() method to execute & get the output of a command-line execution.
$execResponse = exec('whoami', $output, $return_var);
echo $execResponse; // This will print the output from command line
print_r($output); // This will have the same output in an array.
$command: This parameter is used to hold the command which will be executed.
$output: This parameter is used to specify the array which will be filled with every line of output from the command.
$return_var: The $return_var parameter is present along with the output argument, then it returns the status of the executed command will be written to this variable.

How to check bash shell finished in php

I am having difficulty processing bash shell in php.
My problem is as below.
I have 3 tasks corresponding to 3 files bash shell (task1.sh, task2.sh, task3.sh). When task1.sh finishes processing, task2.sh will automatically execute, when task2.sh finishes processing, task3.sh will automatically execute.
Initially, I wrote a file called task.sh and embedded task1.sh, task2.sh, task3.sh into it. But I want to embed these 3 tasks into a php file.
For example: I create task.php and do the following:
If task1.sh fails, it will display popout (alert) error message.
If task1.sh processing is complete then task2.sh will continue to be automatically done.
The processing of task2.sh and task3.sh is similar to the above.
All 3 tasks I want to run backgroud. The problem is that when I run the background bash shell, I will not be able to check the failed error statement (the result always returns to 0).
I have learned a lot and consulted many documents but it did not help me.
I hope you can support me.
Sorry, my english very poor.
You may use the $retval argument of exec().
<?php
exec('task1.sh', $output, $retval);
if ($retval !== 0) {
// task 1 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task2.sh', $output, $retval);
if ($retval !== 0) {
// task 2 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
exec('task3.sh', $output, $retval);
if ($retval !== 0) {
// task 3 failed
exit('Error running task1: ' . implode("<br/>\n", $output));
}
You can simply use the function:
exec ( string $command [, array &$output [, int &$return_var ]] ) : string
With:
output :
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
return_var :
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Thus you could use $output and $result_var to check the execution errors of your shells.
Regards

executing curl cmd in php should return json but instead returns array

Just wondering if there is a setting that I am missing in my curl command that is preventing the data from coming back as a json string and instead returning it as an array.
$cmd='curl -d #/home/wazilly/public_html/recipe.json -H "Content-Type:
application/json" "https://api.edamam.com/api/nutrition-details?
app_id=XXXXX&app_key=97d3b48d3a8366572c8012a142e28f50"';
exec($cmd,$result);
echo $result; //prints Array
According to the documentation, exec always takes an array as its second parameter, which it fills with all the lines of output of the executed command.
I'm not certain what the output of your command would be like, though. You'd be better off doing a var_dump() on the $result to be certain. But since you're dealing with curl, why not simply use the PHP curl extension ?
Check the prototype for exec() in the PHP manual:
string exec ( string $command [, array &$output [, int &$return_var ]] )
PHP returns an array. If you want the raw output instead, which I assume might actually be JSON, try this instead:
$result = shell_exec($cmd);
echo $result;
I suggest that you convert $result array to JSON format using json_encode(array())
echo json_encode($result);

Count string occurance and assign output parts variables

Good Day everyone
Having a bit of a "Head Bang"on this. Im trying to find a way to search through files for a certain string. I know how to do it in linux by using a simple grep -c, but i'm unable to assign varaibles to the output. For example I want the file name as $var1 and the count as $var2, the reason for this is I will be using the data in a chart. I'm not to fixed on using linux commands, if there is any better way to do so I am open to using it. I would appreciate any info or direction of where to go and read up. My code:
<?php
$output = shell_exec ( "grep -c 'detected Tag 10055' *.txt" )
?>
This would the give me the file path and count as ../../public/130512.txt:100.
Thanks for any info
Use the another method in PHP to capture the output of the external program
string exec ( string $command [, array &$output [, int &$return_var ]] )
For example
$command = 'ls -ltr';
$response = exec ( $command, $output , $return );
print_r($output);
print_r($return);
The variable output array will contain your execution output, from that you can take the result.
The $response contains your last line of external program's output
$output contains the array of all output line by line
$return is the return value of your external program

svnlook through php gives one line

I have a simple php exec command that calls svnlook. If I run the command through the terminal I get all the output I expect. If I run it as shown below, I only get the last item.
$list = exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT);
echo $list;
Can I buffer the output? If so how? And will that help?
That's by design and is explained:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
http://php.net/manual/en/function.exec.php
exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT, $output);
var_dump($output);
Alternatively, shell_exec returns everything.

Categories