Home > Backend Development > PHP Tutorial > A simple example of PHP receiving post data and parsing json

A simple example of PHP receiving post data and parsing json

WBOY
Release: 2016-07-25 08:58:12
Original
1564 people have browsed it
This article introduces an example of PHP receiving the data from the post and parsing json for your reference.

php receives post data and parses json code, using php functions--stripslashes, json_decode, var_dump:

<?php
$json_string = $_POST["txt_json"];
if(ini_get("magic_quotes_gpc")=="1")
{
$json_string=stripslashes($json_string);
}
$user = json_decode($json_string);
echo var_dump($user);
?>
Copy after login

Code description: First, get the value of the POST form field txt_json in the html file, put it into the variable $json_string, and then make a judgment. If the current PHP setting is magic_quotes_gpc=On, that is, the incoming double quotes will be escaped, so the json_decode function Cannot be parsed, so it needs to be unescaped. After anti-escaping, use the json_decode function to convert the JSON text into an object, save it in the $user variable, and finally use echo var_dump($user); to dump the object.

Attached, PHP JSON data parsing code

PHP parses JSON data code, it always knows how to parse API data in various transmission formats, including JSON, XML, etc. The following code parses the data obtained by POST from the Renren Connection website:

<?php
$json_string='{"id":1,"name":"jbxue","email":"admin@jbxue.com","interest":["wordpress","php"]} ';
$obj=json_decode($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php
?>
Copy after login


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template