I'm having trouble receiving JSON data in PHP to save to the database, and this data is sent from a python script , I already tried json_decode
using the $_REQUEST
variable, but without success, when I write to a .txt
I can get the data but when I try to write to a variable to save in the bank I can not print anything to confirm that the data was for variable.
Python
import json
import requests
import Adafruit_DHT
import RPi.GPIO as GPIO
import Adafruit_BMP.BMP085 as BMP085
import time
sensorBMP = BMP085.BMP085();
sensorDHT11 = Adafruit_DHT.DHT11;
GPIO.setmode(GPIO.BOARD);
pinoSensorDHT11 = 25;
temperature = 0.0;
pressure = 0.0;
humidity = 0.0;
dia = 0;
hora = 0.0;
while(1):
temperature = sensorBMP.read_temperature();
pressure = sensorBMP.read_pressure();
altitude = sensorBMP.read_altitude();
dia = time.strftime("%d/%m/%Y");
hora = time.strftime("%H/%M/%S");
while(1):
humidity, tempNotUsed = Adafruit_DHT.read_retry(sensorDHT11,pinoSensorDHT11);
if humidity is not None and tempNotUsed is not None:
dic = {
'Temperature' : temperature,
'Humidity' : humidity,
'Pressure' : pressure,
}
jason = json.dumps(dic);
print(jason);
r = requests.post("http://54.174.181.91/tcc/jsonTeste.php", data = {"jsonFile": jason});
#print (r);
if r.status_code is not 200:
print "FALHA!!!!!!";
print r.status_code;
break;
break;
break;
And when I write to txt using PHP:
<?php
echo "<pre>";
var_dump($_POST);
$data = $_POST["jsonFile"];
// Abre ou cria o arquivo bloco1.txt
// "a" representa que o arquivo é aberto para ser escrito
$fp = fopen("bloco1.txt", "w");
// Escreve "exemplo de escrita" no bloco1.txt
$escreve = fwrite($fp, $data);
// Fecha o arquivo
fclose($fp);
?>
How can I get this data and write to a common variable?