How to use File_get_contents ("Php://input")

Source: Internet
Author: User
$data = file_get_contents ("Php://input");
Php://input is a read-only stream that can access the requested raw data. In the case of POST requests, it is best to use php://input instead of $HTTP _raw_post_data because it does not depend on specific php.ini directives. Also, in such cases $HTTP _raw_post_data is not populated by default, and less memory is potentially needed than activating always_populate_raw_post_data. Enctype= "Multipart/form-data" when the Php://input is invalid.


1, Php://input can read the value of the specified length in the HTTP entity body, by specifying the length of the content-length, either by post or by the data submitted by the Get method. However, when the general get method submits the data, the HTTP request Entity body section is empty.
2,php://input and $http_raw_post_data read the same data and read only Content-type data that is not multipart/form-data.
Learn notes
1,coentent-type only if the value is application/x-www-data-urlencoded and Multipart/form-data two, PHP will fill the HTTP request packet with the corresponding data in the global variable $_ POST
2,php Unrecognized content-type type, the corresponding data in the HTTP request package is filled into the variable $http_raw_post_data
3, PHP will not fill in the HTTP request packet of the corresponding data in the Php://input, otherwise, only when Coentent-type is Multipart/form-data. The length of the fill, specified by Coentent-length.
4, php://input data is consistent with $_post data only when Content-type is application/x-www-data-urlencoded.
5,php://input data is always the same as $http_raw_post_data, but php://input is more effective than $http_raw_post_data and does not require special settings php.ini
6,php fills the query_path portion of the path field into the global variable $_get. Normally, the body is empty for the HTTP request submitted by the Get method.

Example
1.php can receive XML data with file_get_contents ("Php://input") or $http_raw_post_data
Like what:
getxml.php;//Receive XML address

1 2 3 4 5 6 7 8 9 A-list <!--? php       $xml = ' <xml-->xmldata '; XML       $url to send = ' http://localhost/test/getXML.php ';//Receive XML address           $header = ' content-type:text/xml '; Define Content-type FOR XML       $ch = Curl_init (); Initialization of Curl       curl_setopt ($ch, Curlopt_url, $url); Set links       curl_setopt ($ch, Curlopt_returntransfer, 1); Set whether to return information       curl_setopt ($ch, Curlopt_httpheader, $header); Set HTTP Headers       curl_setopt ($ch, Curlopt_post, 1); Set to post mode       curl_setopt ($ch, Curlopt_postfields, $xml); Post data       $response = curl_exec ($ch); Receive return information       if (Curl_errno ($ch)) {//Error displays error message       print Curl_ Error ($CH);      }       curl_close ($ch); Close Curl links       Echo $response; Display return information?>


2. A mobile phone upload pictures to the server applet
Uploading files

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25 <!--? php      //@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} HT" Tp/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)) {     & nbsp;    $d. = Fgets ($fp, 4096);         }          fclose ($FP);          Echo $d;      }?-->


Receiving files

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 <!--? php         /**            *recieve image data          **/              error_reporting (E_all);          function get_contents () {              $xmlstr = file_get_contents ("Php://input");          $filename =time (). '. png ';          if (file_put_contents ($filename, $xmlstr)) {   & nbsp;      Echo ' success ';         } else {          Echo ' failed ';         }         }           get_contents ();?-->

3. Get HTTP Request Original
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 /** * 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

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.