[PHP] several cases of JSON value passing and receiving by PHP

Source: Internet
Author: User
: This article mainly introduces several cases of JSON value passing and receiving in PHP. For more information about PHP tutorials, see. The background network of Chrome is used to analyze $. Ajax() Several situations when the json data is post to PHP:

  • Json data cannot be obtained through $ _ POST and $ _ REQUEST in PHP, that is

    $ Json = $ _ POST ['json']; // empty ($ json) is 1

Note: By default, PHP only recognizes the standard data types of application/x-www.form-urlencoded. Therefore, content such as text/xml or soap or application/octet-stream cannot be parsed, if you use the $ _ POST array to receive the message, it will fail.

Case:

The contentType attribute is not added to js: "application/json; charset = utf-8 ",

Var submit_sync = function () {$. ajax ({type: "post", url: 'Add-post-json.php ', async: false, // use the synchronous method // 1 needs to use JSON. stringify otherwise the format is a = 2 & B = 3 & now = 14... // 2 requires forced type conversion. Otherwise, the format is {"a": "2", "B": "3"} data: JSON. stringify ({a: parseInt ($ ('input [name = "a"] '). val (), B: parseInt ($ ('input [name = "B"] '). val (), now: new Date (). getTime () // do not add a comma to this line}), dataType: "json", success: function (data) {$ ('# result '). text (data. result);} // do not add a comma in this line });}

  • After $ GLOBALS ['http _ RAW_POST_DATA '] is used, data cannot be obtained, that is

$ Json = $ GLOBALS ['http _ RAW_POST_DATA ']; // empty ($ json) is 1

  • Use file_get_contents ("php: // input"); to obtain data, that is

$ Json = file_get_contents ("php: // input"); // empty ($ json) is 0

Case B:

Add the attribute contentType in js: "application/json; charset = utf-8 ",

Var submit_sync = function () {$. ajax ({type: "post", url: 'Add-post-json.php ', async: false, // use the synchronous method // 1 needs to use JSON. stringify otherwise the format is a = 2 & B = 3 & now = 14... // 2 requires forced type conversion. Otherwise, the format is {"a": "2", "B": "3"} data: JSON. stringify ({a: parseInt ($ ('input [name = "a"] '). val (), B: parseInt ($ ('input [name = "B"] '). val (), now: new Date (). getTime () // do not add a comma to this line}), contentType: "application/json; charset = utf-8 ",
DataType: "json", success: function (data) {$ ('# result '). text (data. result);} // do not add a comma in this line });}

  • Use $ GLOBALS ['http _ RAW_POST_DATA '] to obtain the data, that is

$ Json = $ GLOBALS ['http _ RAW_POST_DATA ']; // empty ($ json) is 0

  • Use file_get_contents ("php: // input"); to obtain data, that is

$ Json = file_get_contents ("php: // input"); // empty ($ json) is 0

Note: php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".

Case C:

Add the following?

header('Content-Type:application/json;charset=utf-8');

There is no impact on the results of case A and case B.

Starting from version 5.2, PHP provides native json_encode () and json_decode () functions. The former is used for encoding and the latter is used for decoding. The blog below is very detailed, mark!

Use JSON: http://www.ruanyifeng.com/blog/2011/01/json_in_php.html in PHP

The above describes several cases of JSON value transfer in [PHP] and receiving in PHP, including some content, and hope to help those who are interested in PHP tutorials.

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.