Receive json data in PHP

9

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?

    
asked by anonymous 09.11.2015 / 12:26

4 answers

1

$_POST gets forms data ( headers of type urlencoded and form ).

You will be able to get the data with

$data = json_decode(file_get_contents('php://input'));

reading the request body directly.

And why did this issue now appear in home of ptSO after almost two years? OP could not solve the problem in two years?

    
06.08.2017 / 09:34
0

I do not understand very well the first doubt, if you can show how the json you're trying to save would be easier.

But for your last question, when you save json to the file, serializes json before storing, because you would generate a serialized data and would be able to work better at json file, because when you le data from a file you can only read line by line. So you would get a single line that would have all your json data, would give a deserialize and could put in a single variable containing all your json.

To read files you can take a look at the php documentation. link

    
09.11.2015 / 13:06
-1

As you are using arduino it does not send the header of the page, you can use

header('Content-Type: application/json');
echo json_encode($data);

is one of the alternatives, the other is you set at the time of saving in the database the type of encoding is using, preferably UTF8

    
09.09.2016 / 00:26
-2

Try this:

$data = json_decode($jsonFile);
    
29.03.2017 / 00:46