PHP post sends and receives data

PHP   Send POST data

$data = '{
    "id": "17999030",
    "method": "sayHello",
    "jsonrpc": "2.0",
    "params": 
        {
            "acmac": "00E0614CA7C6",
            "acconf_version": "2015-10-28-09-45"
        }
    }';
$url = "http://wifi.doucube.com/index.php/interface/device/ConfHeartbeat.html";

$res = http_request($url, $data);

var_dump($res);

//HTTP request (supports HTTP/HTTPS and GET/POST)
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}$data = '{
    "id": "17999030",
    "method": "sayHello",
    "jsonrpc": "2.0",
    "params": 
        {
            "acmac": "00E0614CA7C6",
            "acconf_version": "2015-10-28-09-45"
        }
    }';
$url = "http://wifi.doucube.com/index.php/interface/device/ConfHeartbeat.html";

$res = http_request($url, $data);

var_dump($res);

//HTTP request (supports HTTP/HTTPS and GET/POST)
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

  

PHP receives POST data

The data type recognized by PHP by default is the data type of the application/x-www.form-urlencoded standard.

1,$_ POST['paramName']   Only when the content type is application/x-www-form-urlencoded or multipart / form data, PHP will fill in the data of the corresponding part of the body in the http request packet$_ In the post global variable, PHP ignores all other cases. Fill in to$_ The data in the post array is the result of urlcode () parsing.

 

2,file_get_contents("php://input ") applies to most types of content types

php://input Allows reading of raw post data. And $http_ RAW_ POST_ Compared with data, it puts less pressure on memory and does not require any special php.ini settings. php://input Cannot be used for enctype = "multipart / form data".

 

3,$GLOBALS['HTTP_RAW_POST_DATA']; Always produce  $ HTTP_ RAW_ POST_ DATA    The variable contains the original post data. This variable is generated only when data of unrecognized MIME type is encountered$ HTTP_ RAW_ POST_ DATA    about   enctype="multipart/form-data"    Form data is not available.

If the post data is not recognized by PHP, you can use $GLOBALS['HTTP_RAW_POST_DATA '] to receive it, such as text/xml or soap. Always in php.ini needs to be set_ populate_ raw_ post_ When the data value is On, PHP will always fill the post data into the variable $http_ raw_ post_ data.

Look at the official documentation. In the later version, this variable is $HTTP_RAW_POST_DATA is deprecated
This feature wasDEPRECATEDin PHP 5.6.0, andREMOVEDas of PHP 7.0.0. In general,php://inputshould be used instead of$HTTP_RAW_POST_DATA.

 

Summary:

1. Coentent type only when the values are application/x-www-data-urlencoded and multipart/form- data, PHP will fill the corresponding data in the http request packet into the global variable$_ POST

2. When PHP does not recognize the content type, it will fill the corresponding data in the HTTP request package into the variable $HTTP_RAW_POST_DATA

3. Only when the coentent type is not multipart / form data, PHP will fill the corresponding data in the http request packet into php: //input, otherwise other situations will occur. The filled length is specified by coentent length.

4. Only when the content type is application/x-www-data-urlencoded, php://input Data just follow$_ Consistent with POST data.

5, php://input Data always follows $http_ RAW_ POST_ The data is the same, and only the data whose content type is not multipart / form data is read, but php://input Than $HTTP_RAW_POST_DATA is more efficient, and php.ini does not need to be specially set

6. PHP will query the PATH field_ In the PATH section, fill in the global variable$_ GET. Generally, the http request submitted by the get method has an empty body.

7, php://input Cannot read$_ GET data. Because$_ GET data as query_ The PATH is written in the PATH field of the http request header, not in the body part of the http request.

8. For application/x-www-form-urlencoded and multipart / form data formats$_ POST;

9. If you can't get it, such as text/xml, application/json, soap, use file_get_contents('php://input ');

 

Posted by contex on Sat, 27 Nov 2021 19:06:02 -0800