the scope of my project is to pass certain values from a python script to a remote php script.
i've a python script that generate an associatve array. For example (already JSON encoded):
{"FRONT": "19.50", "RACK": "17.63", "REAR": "21.06", "ROOM": "15.6"}
I need to pass this associative array to a remote PHP script i followed this tutorial: http://nonstopblah.wordpress.com/2010/07/13/python-to-php-via-json/
I get 200 for HTTP Response but in the php script the POST variable seems to be empty
Here my code:
bulkData = json.dumps(temp, 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
print bulkData
print '\nHTTP Response'
headers = { "charset":"utf-8",
"Accept": "text/plain"}
conn = httplib.HTTPConnection(report_host)
postData = urllib.urlencode({'results':bulkData})
conn.request("POST", report_path, postData,headers)
response = conn.getresponse()
text = response.read()
print "Response status: ",response.status,"\n",text
conn.close()
this is the PHP script:
if( isset($_POST['results']) )
{
$data = json_decode($_POST['results']);
print_r($data);
}
else
{
echo 'Nothing to listen.';
print_r($_POST);
}
and this is the output of my python script (with the remote response):
{"FRONT": "20.44", "RACK": "18.88", "REAR": "21.25", "ROOM": "17.7"}
HTTP Response
Response status: 200
Nothing to listen.Array
(
)
is there a smarter way to do it? What am i missing here?
Thank you in advance for your kind answers.
Content-Typereceived by the PHP script? You're sending "application/x-www-form-urlencoded" data, and if that arrives as, say, "text/plain", it may not work. (But really, why are you URL-encoding the data in the first place? Why not just send "application/json" and send the JSON as-is as the POST data?)