0

I have Raspberry Pi RFID reader which is collecting simple ID numbers from tags nearby. Those are stored as a string variable. I want to continuously send this string value over to PHP script and display it on the web page and then store it in database. How do I pass this string value over using JSON in python?

Python Script:

import serial, os, httplib, json, urllib

#pyserial setup
tagID = tagID[:8] #true string tagID - 73203842

#JSON setup
headers = { "charset" : "utf-8", "Content-Type" : "application/json" }
conn = httplib.HTTPConnection("192.168.XX.XXX")

sample = { "ID" : tagID }
sJson = json.dumps(sample, ensure_ascii = 'False')

while True:
    conn.request("POST", "/test.php", sJson, headers)
    response = conn.getresponse()
    print(response.read())

PHP File:

<?php
$data = json_decode($_POST['results']);
echo($data);
?>
  • 1
    do json_decode($_POST['results'],true) and do var_dump($data); in php script./ – Dimi Mar 27 '17 at 17:56
  • @Dimi it says results Undefined index: results –  Mar 27 '17 at 20:41
  • which means that your PHP script is not getting $_POST['results']; . Do var_dump($_REQUEST) to see everything that your script is getting. – Dimi Mar 27 '17 at 20:47
  • apart from the error when I try `var_dump($_REQUEST)` it gives `array(0) { }` –  Mar 27 '17 at 20:50
  • which means that your Python script is not posting anything to your php script. Try code from this answer and see if it works http://stackoverflow.com/questions/11322430/python-how-to-send-post-request – Dimi Mar 27 '17 at 20:53
  • @Dimi this code gives me: `(200, 'OK')` `...` `` –  Mar 28 '17 at 08:53

0 Answers0