Home  >  Article  >  Backend Development  >  PHP gets data in different formats in HTTP POST

PHP gets data in different formats in HTTP POST

藏色散人
藏色散人forward
2019-04-27 09:43:122806browse

The POST method in the HTTP protocol has multiple formats of data protocols, which are identified by different Content-types in the HTTP header. Commonly used ones are

application/x-www-form- urlencoded, which is the most common, is the format of the from form. In the HTTP head, it is Content-Type: application/x-www-form-urlencoded.

multipart/form-data, this is used to upload files, in the HTTP head is Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Raw This is not particularly commonly used. The transmitted data has only one segment in the HTTP body and is not stored in the form of key-value pairs. In the HTTP head, it is Content-Type: application/json, Content-Type: text,Content-Type: application/xml,Content-Type: text/xml, etc. For

Content-Type: application/x-www-form-urlencoded

The data of this form can be obtained directly in php using $_POST['name']. There is nothing Special

Content-Type: multipart/form-data;

Data in this format can be obtained by using $_POST['name'] in php Data can be obtained using $_FILES['file'].For data in Raw format, it cannot be obtained using the above two methods, and other methods need to be used.

1. Use

file_get_contents("php://input")

Get; write a simple php file to test it<pre class="brush:php;toolbar:false">&lt;?php $test=file_get_contents(&quot;php://input&quot;); echo $test;</pre>Test it with postman

PHP gets data in different formats in HTTP POSTNo problem, you can receive it

2. Use

$GLOBALS['HTTP_RAW_POST_DATA']

Receive<pre class="brush:php;toolbar:false">&lt;?php $test=$GLOBALS[&amp;#39;HTTP_RAW_POST_DATA&amp;#39;]; echo $test;</pre>Test it with postman

PHP gets data in different formats in HTTP POST Damn it, something went wrong, the prompt was not found

HTTP_RAW_POST_DATA

What the hell is this array index? After some Google, I saw this paragraph on the official website of PHP Words

PHP gets data in different formats in HTTP POSTIt turns out that

HTTP_RAW_POST_DATA

has been abandoned in php5.6 and has been deleted in php7.0 and later versions. I use The php version is 7.2, something must be wrongOkay, then just use

file_get_contents("php://input")

Get it In actual development, frameworks are generally used. I use thinkphp more. In tp5.0, you can use the getInput() function of Request to obtain the data in Raw

<?php

namespace app\index\controller;

use think\Request;

class Index
{
    public function index(Request $request)
    {
        echo $request->getInput();
    }
}

Test it

PHP gets data in different formats in HTTP POSTNo problem, you can obtain it normally

The method of obtaining HTTP POST data in PHP will be introduced here. I hope it will be helpful to friends in need!

The above is the detailed content of PHP gets data in different formats in HTTP POST. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:aliyun.com. If there is any infringement, please contact admin@php.cn delete