douhua1890 2016-01-12 20:42
浏览 427
已采纳

从php调用python,返回值为2

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

  • 写回答

2条回答 默认 最新

  • duangu1645 2016-01-12 20:45
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • drkbpwk609294 2016-01-12 20:51
    关注

    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);
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥30 单片机实验pc软件完全,可以私我商议
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
  • ¥20 h3c静态路要求有详细过程
  • ¥15 调制识别中输入为时频图,星座图,眼图等
  • ¥15 数据结构C++的循环、随机数问题