Reputation: 2203
I have php program, that executes a test program with a parameter. That returns json data. I tested with print_r what I get and use a foreach loop to get each row.
$jsondata = shell_exec("/bin/test node");
$data = json_decode($jsondata, true);
...
print_r($data);
/* output:
Array
(
[nodes] => Array
(
...
*/
foreach ($data['nodes'] as $node) {
...
The result should be a table in html. The command line prints out that table as expected. If I try to execute that script via web I get an error:
The error in the log file:
2019/04/02 18:41:33 [error] 1482#1482: *14607 FastCGI sent in stderr: "PHP message: PHP Warning: Invalid argument supplied for foreach() in /media/... xxx.com/test.php on line 85" while reading response header from upstream, client: 192.168.178.1, server: xxx.com, request: "GET /Lightning/listnodes.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "xxx.com"
If I pipe the test program to 1.html and display it in a web browser, it works.
Upvotes: 0
Views: 53
Reputation: 29932
shell_exec
is executed in the context of the web-server if the script is invoked through that web-server (i.e. via web request). You probably have the permission to execute /bin/test node
when logged in via SSH to the server, but the web-server software (e.g. Apache or nginx) maybe does not have the permission to run /bin/test node
. Check the permissions for the command.
See also: https://www.php.net/manual/en/function.shell-exec.php#37971
Upvotes: 1