jimbolino
jimbolino

Reputation: 95

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

Upvotes: 1

Views: 357

Answers (2)

jimbolino
jimbolino

Reputation: 95

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

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

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

Upvotes: 2

Related Questions