Reputation: 81
What I want to do here is to send json data from python program and receive in php and store it in database and show it in a browser.
This is the code I am using for sending the json data from python :
import httplib,json,urllib
headers = { "charset":"utf-8",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("localhost")
#converting list to a json stream
bulkData={"temp_value":123}
bulkData = json.dumps(bulkData, ensure_ascii = 'False')
# ensure_ascii is false as data is in unicode and not ascii encoding , use this if data is in any other encoding
postData = urllib.urlencode({'results':bulkData})
conn.request("POST", "/test1.php", postData,headers)
response = conn.getresponse()
text = response.read()
print response.status,text
conn.close()
These are the php codes that i used to receive the json data from python : opt 1)
<?php
if (isset($_POST['results']))
{
$data = json_decode($_POST['results']);
// This is to decode the json stream, using a function called json_decode()
foreach($data as $record) // ACCESS each record individually
{
foreach($record as $key => $value)
{
echo $key . '->' .$value;
// you can get individual key , value pairs (if content is in dictionary format)
}
}
}
else
{
echo $data;
echo 'POST Variable not found';
}
?>
opt 2)
<?php
$url = "http://localhost/pyjson.py";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo "temp value: ". $json_data["temp_value"];
?>
When I run the python using option 1 i get like 200 POST Variable not found
in the python side and in the php side i get like POST Variable not found
.
When I run the same program using opt 2 i will get like this in python
200 <br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: file_get_contents(http://localhost/pyjson.py): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in C:\wamp\www\test1.php on line <i>3</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>240472</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\test1.php' bgcolor='#eeeeec'>..\test1.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>240704</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.file-get-contents' target='_new'>file_get_contents</a>
( )</td><td title='C:\wamp\www\test1.php' bgcolor='#eeeeec'>..\test1.php<b>:</b>3</td></tr>
</table></font>
And in the php side I got
Warning: file_get_contents(http://localhost/pyjson.py): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\test1.php on line 3
What I want to do here is to send json data from python program and receive in php and store it in database and show it in a browser. Someone please suggest me a proper way for this program and to do this.
Upvotes: 5
Views: 9816
Reputation: 355
Option 1 is pretty close, however it looks like you are trying to URL encode the POST request you are creating from Python, and then decoding that into JSON, which unfortunately will not work. What you want to do is send the JSON itself as the POST request's body, and then consume that on your PHP server.
Create a file called provider.py and use this as it's content:
import httplib, json
headers = { "charset" : "utf-8", "Content-Type": "application/json" }
conn = httplib.HTTPConnection("localhost")
sample = { "temp_value" : 123 }
sampleJson = json.dumps(sample, ensure_ascii = 'False')
# Send the JSON data as-is -- we don't need to URL Encode this
conn.request("POST", "/consumer.php", sampleJson, headers)
response = conn.getresponse()
print(response.read())
conn.close()
Then, set up a PHP web server, and create a file named consumer.php at it's root, with this content:
<?
// Function to print out objects / arrays
function PrintObj ($o) { echo "<pre>"; print_r($o); echo "</pre>"; }
// Load the POST.
$data = file_get_contents("php://input");
// ...and decode it into a PHP array.
$data = json_decode($data);
// Do whatever with the array.
PrintObj($data);
From here, if you run
$ python provider.py
You should get the following output to the console:
<pre>stdClass Object
(
[temp_value] => 123
)
</pre>
Upvotes: 3