Passing JSON to PHP not working

Question:

I’ve been trying to get my python code to transfer SQL query data from client running python to web server running PHP, then input that data into a MySQL database.

The below is the python test code simulating a single row:

    #!/usr/bin/python

import requests
import json


url = 'http://192.168.240.182/insert_from_json.php'
payload = {"device":"gabriel","data_type":"data","zone":1,"sample":5,"count":0,"time_stamp":"00:00"}
headers = {'content-type': 'application/json'}

response = requests.post(url, data=dict(payload=json.dumps(payload)), headers=headers)
print response

The below is the PHP script on the server side:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";

$payload_dump = $_POST['payload']
//$payload = '{"device":"gabriel","data_type":"data","zone":1,"sample":4,"count":0,"time_stamp":"00:00"}';

$payload_array = json_decode($payload_dump,true);

//get the data_payload details
$device = $payload_array['device'];
$type = $payload_array['data_type'];
$zone = $payload_array['zone'];
$sample = $payload_array['sample'];
$count = $payload_array['count'];
$time = $payload_array['time_stamp'];

$sql = "INSERT INTO data(device, data_type, zone, sample, count, time_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>

If I comment out $payload_dump and instead use $payload and run the script the row is added correctly. But when I call the python script on the client I get a “response 200” but the record was not added. Something either my JSON string in python isn’t formatted correctly or I am not passing the value correctly.

1) What is the syntax/procedure on the python code that would allow me to see what has actually been received by the PHP code. (i.e. convert the “$payload_dump” variable contents to a string and send back to the python script?

2) How do I resolve the above code?

Asked By: ghowe

||

Answers:

Your first problem is using json headers which you shouldn’t use since you aren’t sending a raw json:

your data look like

{payload = {'key1': 'value1',
            'key2': 'value2' }
         }

and a raw json data should be in the form:

{'key1': 'value1', 
 'key2': 'value2'}

So you need to remove the headers from the request

response = requests.post(url, data=dict(payload=json.dumps(payload)))

Second you need to fix that missing semicolon

$payload_dump = $_POST['payload']

to

$payload_dump = $_POST['payload'];

Better solution

You can send the json data directly using requests

response = requests.post(url, json=payload)

then grab it in php with

$payload_array = json_decode(file_get_contents('php://input'), true);
Answered By: Chaker
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.