Home >Backend Development >PHP Tutorial >PHP handles json data from Python's Post

PHP handles json data from Python's Post

WBOY
WBOYOriginal
2016-08-08 09:24:031358browse
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()

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()

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);
    }
?>

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:
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