File_get_contents ("php: // input") usage

Source: Internet
Author: User
File_get_contents (php: input) usage $ 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 rely on specific php. ini commands.
In this case, $ HTTP_RAW_POST_DATA is not filled by default, which requires less memory than activating always_populate_raw_post_data.
When enctype = "multipart/form-data", php: // input is invalid.


1. php: // input can read the value of the specified Length in the http entity body, which is determined by Content-Length, whether it is data submitted by POST or GET methods. However, when the GET method submits data, the http request entity body is empty.
2. php: // input and $ HTTP_RAW_POST_DATA read the same data and only read data whose Content-Type is not multipart/form-data.

Study Notes
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: // the input data is more efficient than $ HTTP_RAW_POST_DATA, and php. ini does not require special settings.
6. PHP will fill in the query_path section of the PATH field with the global variable $ _ GET. Normally, the http request submitted by the GET method has no body.

Example
1. php uses file_get_contents ("php: // input") or $ HTTP_RAW_POST_DATA to receive xml data.
For example:
GetXML. php; // receives the XML address

$ Xmldata = file_get_contents ("php: // input ");
$ Data = (array) simplexml_load_string ($ xmldata );
?>

$ Data here is an array containing xml data. the detailed method for php to parse xml data updates
SendXML. php

$ Xml =' Xmldata '; // The xml to be sent
$ Url = 'http: // localhost/test/getXML. php'; // receives the 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); // Sets whether to return information.
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // sets the HTTP header
Curl_setopt ($ ch, CURLOPT_POST, 1); // Set it to POST
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml); // POST data
$ Response = curl_exec ($ ch); // receives the returned information
If (curl_errno ($ ch) {// error message displayed
Print curl_error ($ ch );
}
Curl_close ($ ch); // Close the curl link
Echo $ response; // display the returned information
?>

2. a cell phone uploads images to the server.
Upload files

// @ File phpinput_post.php
Using 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 = '1970. 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 ");
Fputs ($ fp, $ http_entity_body. "\ r \ n ");

While (! Feof ($ fp )){
$ D. = fgets ($ fp, 4096 );
}
Fclose ($ fp );
Echo $ d;
}
?>

Receive files

/**
* Recieve image data
**/
Error_reporting (E_ALL );

Function get_contents (){
$ Xmlstr = file_get_contents ("php: // input ");
Using filename1_time().'.png ';
If (file_put_contents ($ filename, $ xmlstr )){
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 rows
$ Raw. = "\ r \ n ";

// (4) request Body
$ Raw. = file_get_contents ('php: // input ');

Return $ raw;
}

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.