2

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?

ghowe
  • 127
  • 1
  • 1
  • 9
  • Have you tried printing the content of `$payload_dump`? It might already show you what's wrong with it. – endofsource Aug 06 '15 at 19:30
  • 1
    seems relevant: http://stackoverflow.com/questions/4214231/sending-data-using-post-in-python-to-php – Mike Bell Aug 06 '15 at 19:40
  • I am not sure how to print the output of the PHP file in the server browser window when the client calls the PHP script. The python script on the client is called by SSH command line. This is why I asked for a way to send the payload data back to python as a response so I can see what it is actually receiving. – ghowe Aug 06 '15 at 19:43
  • I would try `data = json.dumps(dict(payload=payload))` instead of `data = dict(payload=json.dumps(payload))`. If you use `echo` in php and then print `response` in pyhton or `response.text` you should see your php script output. For more information about the `request.Response` http://docs.python-requests.org/en/latest/api/#requests.Response – endofsource Aug 06 '15 at 19:48

1 Answers1

3

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);
Chaker
  • 1,197
  • 9
  • 22