Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing value from python to php

Tags:

python

php

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:

if led is on, send on to php,

if led is off, send off to php,

PHP:

display [value receive from python]

like image 556
syafiqul haziq Avatar asked Sep 29 '22 15:09

syafiqul haziq


1 Answers

If you want to call php script directly:

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()
like image 170
Esref Avatar answered Oct 03 '22 01:10

Esref