PHP handles json data from Python's Post
Release: 2016-08-08 09:24:03
Original
1353 people have browsed it
I recently used Python to process some json data, but encountered some problems during the process, so I recorded them.
1. Python Post json format data to the server: I checked some information, most of which are like this: __author__ = 'jiezhi'
import urllib
import urllib2
data = {'name': 'jiezhi', 'age': '24'}
ret = urllib2.urlopen(url='http://jiezhiblog.com/test.php', data=urllib.urlencode(data))
print ret.read()
Copy after login
However, when it comes to php, it is often of array type. After several twists and turns, I changed to the following code: __author__ = 'jiezhi'
import urllib2
import json
data = {'name': 'jiezhi', 'age': '24'}
ret = urllib2.urlopen(url='http://jiezhiblog.com/test.php', data=json.dumps(data))
print ret.read()
Copy after login
2. Problem on the PHP side
I used the modified Python code, but found that $_POST did not get the data, so I used 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);
}
?>
Copy after login
The submitted data can be obtained correctly at this time. Initial address: http://jiezhiblog.com/archives/366
The above introduces PHP's processing of json data from Python's Post, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31