PHP handles JSON data from the post of Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.