Another method of using file_get_contents ()

Source: Internet
Author: User
: This article mainly introduces another method of using file_get_contents (). If you are interested in the PHP Tutorial, refer to it. $ 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 note 1, Coentent-Type only when the value is application/x-www-data-urlencoded and 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, the corresponding data in the http request packet will be filled in the variable $ HTTP_RAW_POST_DATA 3. PHP will not fill in the corresponding data in the http request packet when the Coentent-Type is multipart/form-data: // 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 no special php settings are required. ini 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. Example 1. php uses file_get_contents ("php: // input") or $ HTTP_RAW_POST_DATA to receive xml data, for example, getXML. php; // to receive XML addresses.
 $ Data here is an array containing xml data. for details about how php parses xml data update, sendXML. php
 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) when an error occurs );} curl_close ($ ch); // Close the curl link echo $ response; // display the returned information?> 2. upload images to the server using a cell phone.
 Receive files
 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 introduces another method of using file_get_contents (), including some content, and hopes to help friends who are interested in PHP tutorials.

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.