简体   繁体   中英

call python from php with 2 return values

I have a python script who is returning 2 values, humidity and temperature:

Temperature: 1.58050537109
Humidity: 89.2761230469

I call this srcipt from a PHP page but only one value is printed

<?php
echo exec("sudo python /home/pi/sht/sht31.py 2>&1");
?>

and only one humidity is printed. Could you please help me to print both value ? Thank you

If you call exec() with only command string, then it will return only the last line of output. To capture everything call it with second parameter to get all output:

exec('command', $output);
var_dump($output);

now output is :

array(5) { [0]=> string(5) "False" [1]=> string(4) "True" [2]=> string(5) "False" [3]=> string(26) "Temperature: 1.56982421875" [4]=> string(23) "Humidity: 89.3859863281" } 

with this code :

exec('sudo python /home/pi/sht/sht31.py' , $output);
var_dump($output);

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