Some JSON data has been processed in Python recently, but there are some problems in the process that have been recorded.
1.Python Post JSON format data to the server:
Looked up some information, mostly this kind of:
__author__ = ' Jiezhi ' import urllibimport urllib2data = {' name ': ' Jiezhi ', ' age ': ' + '}ret = Urllib2.urlopen (url= ' http://j Iezhiblog.com/test.php ', Data=urllib.urlencode (data)) print Ret.read ()
However, to PHP is often the type of array.
After a few tossing, use the following code:
__author__ = ' Jiezhi ' import urllib2import jsondata = {' name ': ' Jiezhi ', ' age ': ' + '}ret = Urllib2.urlopen (url= ' Http://jie Zhiblog.com/test.php ', data=json.dumps (data)) print Ret.read ()
2. Problems on the PHP side
Using the modified Python code, but found that $_post did not get the data, so instead of file_get_contents ("Php://input") to obtain the submitted data:
<?php $input = file_get_contents ("Php://input"); Var_dump ($input); if ($input) { print_r ($input); $arr = Json_decode ($input, true); echo "arr"; Print_r ($arr); }? >
The submitted data can be obtained correctly at this time.
Starting address:http://jiezhiblog.com/archives/366
PHP handles JSON data from a Python post