将值从python传递给php

时间:2021-12-25 17:04:34

I'm having a problem with a project. I need to do a detection system which uses python to communicate with electronic devices. I'm creating a detection system. The problem is that I want to detect and then send to a php file which serves as my user interface.

我的项目有问题。我需要做一个使用python与电子设备通信的检测系统。我正在创建一个检测系统。问题是我想检测然后发送到作为我的用户界面的php文件。

Python:

if led is on, send on to php,

如果led已打开,请发送到php,

if led is off, send off to php,

如果led已关闭,请发送给php,

PHP:

display [value receive from python]

显示[从python接收的值]

3 个解决方案

#1


2  

If you want to call php script directly:

如果你想直接调用php脚本:

php code:

 <?php
 $state = $argv[1];
 echo $state;
 ?>

python code:

from subprocess import *
#ledstate='on'
p = Popen(['/usr/bin/php','<php file name>',ledstate],stdout=PIPE)
print p.stdout.read()

If you want to call via server:

如果你想通过服务器打电话:

php code:

<?php
$state = $_GET["led"];
echo $state;
?>

python code:

import urllib2
#ledstate='on'
req = urllib2.Request(url='http://example.com/<php file name>?led=%s' % ledstate )
f = urllib2.urlopen(req)
print f.read()

#2


0  

You can use subprocess.check_call, to call the php command passing the variable from python:

你可以使用subprocess.check_call来调用从python传递变量的php命令:

from subprocess import check_call

check_call(["list","of","php" ,"commands","off/on"])

If you want to store the output use check_output

如果要存储输出,请使用check_output

#3


0  

now i already got a way to retrieve data form python using php..

现在我已经有了一种方法来使用PHP从python中检索数据..

<?php
$result = exec("sudo python /home/pi/detect_led.py");
$resultData = json_decode($result);

print $resultData;
?>

but the problem is..the data is slow..i mean..when there's changes i have to refresh 2 time for it to change its value..so is there any where i can change to make it correct..?

但问题是......数据很慢..我的意思是......当有变化时我必须刷新2次才能改变它的价值。那么我有什么地方可以改变以使其正确...?

#1


2  

If you want to call php script directly:

如果你想直接调用php脚本:

php code:

 <?php
 $state = $argv[1];
 echo $state;
 ?>

python code:

from subprocess import *
#ledstate='on'
p = Popen(['/usr/bin/php','<php file name>',ledstate],stdout=PIPE)
print p.stdout.read()

If you want to call via server:

如果你想通过服务器打电话:

php code:

<?php
$state = $_GET["led"];
echo $state;
?>

python code:

import urllib2
#ledstate='on'
req = urllib2.Request(url='http://example.com/<php file name>?led=%s' % ledstate )
f = urllib2.urlopen(req)
print f.read()

#2


0  

You can use subprocess.check_call, to call the php command passing the variable from python:

你可以使用subprocess.check_call来调用从python传递变量的php命令:

from subprocess import check_call

check_call(["list","of","php" ,"commands","off/on"])

If you want to store the output use check_output

如果要存储输出,请使用check_output

#3


0  

now i already got a way to retrieve data form python using php..

现在我已经有了一种方法来使用PHP从python中检索数据..

<?php
$result = exec("sudo python /home/pi/detect_led.py");
$resultData = json_decode($result);

print $resultData;
?>

but the problem is..the data is slow..i mean..when there's changes i have to refresh 2 time for it to change its value..so is there any where i can change to make it correct..?

但问题是......数据很慢..我的意思是......当有变化时我必须刷新2次才能改变它的价值。那么我有什么地方可以改变以使其正确...?