PHP input Stream Php://input example explain _php skills

Source: Internet
Author: User
Tags curl

For the php://input introduction, the PHP official manual document has a very clear overview of it in a word.
"Php://input allows to read raw POST data. It is a less memory intensive alternative to$http_raw_post_data and does no need any special php.ini. Php://input is isn't available with enctype= "Multipart/form-data".

Translation over, is this:
"Php://input can read post data that has not been processed. Compared to $http_raw_post_data, it brings less pressure on memory and does not require special php.ini settings. Php://input cannot be used for Enctype=multipart/form-data "
Summarized as follows:

    • 1), Coentent-type only in application/x-www-data-urlencoded and multipart/form-data two cases, PHP will be the HTTP request packet in the corresponding data into the global variable $_ POST
    • 2, PHP can not recognize the type of Content-type, the HTTP request package will be filled with the corresponding data in the variable $http_raw_post_data
    • 3), only when Coentent-type is Multipart/form-data, PHP does not fill in the HTTP request packet of the corresponding data into the php://input, otherwise will be. The length of the fill, specified by Coentent-length.
    • 4), only when Content-type is application/x-www-data-urlencoded, php://input data is consistent with $_post data.
    • 5), php://input data is always the same as $http_raw_post_data, but php://input more effective than $http_raw_post_data, and do not need special settings php.ini
    • 6, PHP will be the path of the Query_path part of the field, fill in the global variable $_get. Normally, the body is empty for the HTTP request submitted by the Get method.

To sum up, when using $_post to get callback data from an app or some interface, use Php://input to try

1. Accept XML data

Sending XML data
$xml = ' <xml>xmldata</xml> ';//the XML $url to send 
= ' http://localhost/test/getXML.php '; Receive XML address 
$header = ' content-type:text/xml ';//define Content-type as XML 
$ch = Curl_init ();//Initialize Curl 
curl_setopt ($ch, Curlopt_url, $url);/set link 
curl_setopt ($ch, Curlopt_returntransfer, 1);//Set whether to return information 
curl_setopt ($ch, Curlopt_httpheader, $header);/Set HTTP header 
curl_setopt ($ch, Curlopt_post, 1);//Set to POST method 
curl_setopt ($ch, Curlopt_postfields, $xml);//post data 
$response = curl_exec ($ch);//Receive return information 
if (Curl_errno ($ch)) {//error message is displayed 
print Curl_error ($ch); 
Curl_close ($ch); Close Curl Link 
echo $response//Display return information 


//php with file_get_contents ("Php://input") or $http_raw_post_ Data can receive
$xmldata = file_get_contents ("Php://input") of XML; 
$data = (array) simplexml_load_string ($xmldata);

2, the mobile phone upload pictures to the server applet
Send

@file phpinput_post.php 
$data =file_get_contents (' btn.png '); 
$http _entity_body = $data; 
$http _entity_type = ' application/x-www-form-urlencoded '; 
$http _entity_length = strlen ($http _entity_body); 
$host = ' 127.0.0.1 '; 
$port =; 
$path = '/image.php '; 
$fp = Fsockopen ($host, $port, $error _no, $error _desc); 
if ($fp) { 
fputs ($fp, "POST {$path} http/1.1\r\n"); 
Fputs ($FP, "Host: {$host}\r\n"); 
Fputs ($FP, "Content-type: {$http _entity_type}\r\n"); 
Fputs ($FP, "content-length: {$http _entity_length}\r\n"); 
Fputs ($fp, "connection:close\r\n\r\n"); 
Fputs ($fp, $http _entity_body. "\r\n\r\n"); 

while (!feof ($fp)) { 
 $d. = Fgets ($fp, 4096); 
} 
Fclose ($FP); 
echo $d; 
}

Receive

/**
 *recieve image data
 **/
error_reporting (e_all);

function get_contents () {
 $xmlstr = file_get_contents ("Php://input");
 $filename =file_put_contentsxmltime (). PNG ';
 if (($filename, $str)) {
 echo ' success ';
 } else{
 Echo ' failed ';
 }
Get_contents ();

3: Get HTTP request text

/**
 * Get HTTP request original
 * @return string
/function Get_http_raw () {
 $raw = ';
 (1) Request line 
 $raw. = $_server[' Request_method ']. ' ' . $_server[' Request_uri ']. ' ' . $_server[' Server_protocol ']. "\ r \ n";
 (2) Request Headers 
 foreach ($_server as $key => $value) {
 if (substr ($key, 0, 5) = = ' Http_ ') {
  $key = s Ubstr ($key, 5);
  $key = Str_replace (' _ ', '-', $key);
  $raw. = $key. ': ' . $value. "\ r \ n";
 }
 }
 (3) Empty line 
 $raw. = "\ r \ n";
 (4) Request Body 
 $raw. = file_get_contents (' php://input ');
 return $raw;
}

The above is for PHP input stream Three small chestnuts, the purpose is to help you more accurately understand the PHP input stream, I hope we have a harvest.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.