简体   繁体   中英

PHP program behaves different at command line and web

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.

shell_exec is executed in the context of the web-server if the script is invoked through that web-server (ie 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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM