Pass Python data to PHP via JSON

The scope of my project is to pass certain values ​​from a python script to a remote PHP script.

I have a python script that generate an associatve array. For example (already encoded JSON):

{"FRONT": "19.50", "RACK": "17.63", "REAR": "21.06", "ROOM": "15.6"} 

I need to pass this associative array to a remote PHP script after this tutorial: http://nonstopblah.wordpress.com/2010/07/13/python-to-php-via-json/

I get 200 for an HTTP response, but in php script the POST variable seems empty

Here is 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 a 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 result of my python script (with deleted answer):

 {"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 this? What am I missing here?

Thank you in advance for your kind answers.

+4
source share
2 answers

I would skip the urlencoded form data. Just send the raw data in the body:

 conn.request("POST", report_path, bulkData,headers) 

Then you can read the request body in PHP with:

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

The bulkData string will do the same as in your php script.

+4
source

I tested this locally with the simplest PHP CGI, and it took only a few seconds to debug the problem:

You are sending URL-encoded data, but you will not configure the Content-Type accordingly. At least with apache2, since it is configured on OS X 10.7, this means that CGI sees the data as one big line, not a bunch of variables. And, obviously, this line has no member 'results'.

Adding 'Content-Type': 'application/x-www-form-urlencoded' to your headers solves the problem. In addition, you can configure the web server to display the default content type for POST, for example, /cgi-bin/* , which has the same network effect.

However, the real question is why are you both JSON encoding and URL encoding in the first place. They both do similar things. Since your temp object is just a dict with strings for all keys and values, you can just encode the URLs and not worry about JSON. Or, conversely, just take the JSON-encoded bulkData and send it as-is as POST data (ideally with 'Content-Type': 'application/json' , although since you are explicitly reading the data and calling json_decode on it, this is not is strictly necessary).

0
source

Source: https://habr.com/ru/post/1444526/


All Articles