PHP gets data in different formats in HTTP POST

藏色散人
Release: 2023-04-06 12:12:02
forward
2880 people have browsed it

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<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php $test=file_get_contents(&quot;php://input&quot;); echo $test;</pre><div class="contentsignin">Copy after login</div></div>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<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php $test=$GLOBALS[&amp;#39;HTTP_RAW_POST_DATA&amp;#39;]; echo $test;</pre><div class="contentsignin">Copy after login</div></div>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();
    }
}
Copy after login

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!

Related labels:
php
source:aliyun.com
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