PHP input stream php: // input instance description

Source: Internet
Author: User
This article mainly introduces the PHP input stream php: input example, for example, the input stream php: input for in-depth learning, interested friends can refer to the introduction of php: // input, the official PHP Manual provides a clear overview of this document.
"Php: // input allows you to read raw POST data. it is a less memory intensive alternative to $ HTTP_RAW_POST_DATA and does not need any special php. ini directives. php: // input is not available with enctype = "multipart/form-data ".

The translation is like this:
"Php: // input can read POST data that has not been processed. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require special php. ini settings. Php: // input cannot be used for enctype = multipart/form-data"
Summary:

  • 1) Coentent-Type can only be set to application/x-www-data-urlencoded or multipart/form-data, PHP will fill in the corresponding data in the http request packet with the global variable $ _ POST
  • 2) When PHP cannot recognize the Content-Type, it will fill in the corresponding data in the http request package with the variable $ HTTP_RAW_POST_DATA
  • 3) when the Coentent-Type is multipart/form-data, PHP will not fill in the corresponding data in the http request packet in php: // input; otherwise, it will be used in other cases. Length specified by Coentent-Length.
  • 4) php: // input data is consistent with $ _ POST data only when the Content-Type is application/x-www-data-urlencoded.
  • 5) php: // the input data is always the same as $ HTTP_RAW_POST_DATA, but php: // input is more efficient than $ HTTP_RAW_POST_DATA, and php. ini does not need to be specially set.
  • 6) PHP will fill in the query_path part of the PATH field with the global variable $ _ GET. Normally, the http request submitted by the GET method has no body.

In summary, when $ _ POST cannot obtain the callback data from the APP or some interfaces, use php: // input.

1. accept xml data

// Send xml data $ xml ='
 
  
Xmldata
 '; // The xml $ url to be sent = '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 the link curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // Set whether to return information curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // Set the HTTP header curl_setopt ($ ch, CURLOPT_POST, 1); // Set it to the POST method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml ); // POST data $ response = curl_exec ($ ch); // receives the returned information if (curl_errno ($ ch )) {// print curl_error ($ ch);} curl_close ($ ch); // Close the curl link echo $ response; // display the returned information // use file_get_contents ("php: // input") or $ HTTP_RAW_POST_DATA to receive xml data $ xmldata = file_get_contents ("php: // input "); $ data = (array) simplexml_load_string ($ xmldata );

2. small programs that upload images to the server via mobile phones
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 = 80; $path = '/image.php'; $fp = fsockopen($host, $port, $error_no, $error_desc, 30); 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 the original HTTP request

/*** Get the original HTTP request * @ 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 = substr ($ key, 5); $ key = str_replace (' _ ','-', $ key); $ raw. = $ key. ':'. $ value. "\ r \ n" ;}}// (3) empty row $ raw. = "\ r \ n"; // (4) request Body $ raw. = file_get_contents ('php: // input'); return $ raw ;}

The above are three small examples of PHP input streams. they aim to help you better understand the PHP input stream and hope you will gain some benefits.

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.