9
I’m having trouble receiving JSON data in PHP to save to the database, and this data is sent from a python script, have tried the json_decode
using the variable $_REQUEST
, but unsuccessfully, when I record in a .txt
i can get the data but when I try to write to a variable to save to the database I can’t print anything to confirm that the data went to 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 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 take this data and write to a common variable?
If
jsonFile
is not present in$_POST
the problem is in python that did not send the values.– rray
But how does it record in txt? Because it records in txt but when I try to bring to some common variable I can not print on the screen, as it could solve?
– Diogo Bergson
When you do
echo $data
what appears?– rray
Then you bring nothing :(
– Diogo Bergson
When vc displays the page source code (Ctrl+u) nothing appears? add these two lines at the beginning and see if any errors appear,
ini_set('display_errors', true); error_reporting(E_ALL);
– rray
Here is the following: Notice: Undefined index: jsonFile in /var/www/html/tcc/jsonTeste.php on line 9
– Diogo Bergson
:Otimooo, a mistake, he says
jsonFile
was not sent. you didecho $_POST['jsonFile'];
orecho $_GET['jsonFile'];
– rray
I think I did wrong, actually I did so: $data = $_POST["jsonFile"]; echo "test" . $data;
– Diogo Bergson
Do it like this and put the result here,
echo '<pre>' print_r($_POST); print_r($_GET);
– rray
I did what you said but it brings 2 empty arrays: Array ( ) Array ( )
– Diogo Bergson
instead of writing to txt, save to.json and use json_encode for an array.
– Ivan Ferrer
I think you are testing the wrong request. Do the following: In Python, after the line
r = requests.post("htt...
addprint r.text
In PHP, add only the line:print_r(json_decode($_POST['jsonFile'], true));
– vmartins