Home  >  Article  >  Backend Development  >  How to accept post value in php

How to accept post value in php

(*-*)浩
(*-*)浩Original
2019-09-09 11:54:005165browse

During this time, I was studying the interface of PHP, using jmeter to simulate sending data to the PHP server, to see how PHP receives the data transmitted by POST. I encountered several problems. After some trouble, I finally understood it and recorded it. :

How to accept post value in php

Here are two commonly used post methods

The first one:

content-type is application/x-www-form-urlencoded, which is the default data format for post. When using jquery's ajax to post data, the default is this way. The data format transmitted in this way is: username =admin&password=123456. (Recommended learning: PHP programming from entry to proficiency)

The most commonly used $_POST method is used when the server receives it. To obtain the username, use $_POST['username']. Get it normally.

Second:

After studying this method for a long time, I found out how to receive and process the data. The content-type is application/json, in php It is impossible to directly receive the data format of application/json using $_POST method. The data type in application/json format is:

{
"username":"admin",
"password":"123455"
}

For this data type posted to the server, In PHP, the data type needs to be received natively through 'php://input' (the post transmitted data mode), and then use json_encode to parse, and then operate , the php code is:

$raw_post_data = file_get_contents('php://input');
$arr = json_decode($raw_post_data,true);
echo $arr['username'];

At this point, you can obtain application/x-www-form-urlencoded format data through $_POST[ 'username'] obtains the format of application/json data.

The above is the detailed content of How to accept post value in php. For more information, please follow other related articles on the PHP Chinese website!

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