我有Raspberry Pi RFID阅读器,它从附近的标签收集简单的ID号码。这些存储为字符串变量。我想继续将此字符串值发送到PHP脚本并将其显示在网页上,然后将其存储在数据库中。如何在python中使用JSON传递此字符串值?
Python脚本:
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文件:
<?php
$data = json_decode($_POST['results']);
echo($data);
?>