Another way to use file_get_contents ()

Source: Internet
Author: User
$data = file_get_contents("php://input");

 Php://Input is a read-only stream that can access the raw data of the request. POST request, it is best to use php://input instead of the $HTTP _raw_post_data because it does not depend on a specific php.ini directive. And, in such a case $HTTP _raw_post_data default Nopadding, than activating always_populate_raw_post_Data potentially requires less memory. 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, specifying the length by content-length, either post or the data submitted by the Get method. However, when the general get method submits data, the HTTP request entity body part is empty.2,Php://Input reads the same data as $http_raw_post_data, and reads only data that Content-type is not multipart/form-data.LearnNotes1,Coentent-TypeOnly if the value isApplication/X-Www-Data-urlencodedAndMultipart/Form-DataIn both cases,PhpWill not behttpThe corresponding data in the request packet is filled in with the global variable$_post2,PhpNoRecognition ofContent-TypeType, thehttpThe corresponding data in the request package is filled in with the variable$HTTP _raw_post_data3OnlyCoentent-TypeForMultipart/Form-DataThe time,PhpWill not behttpThe corresponding data in the request packet is filled inPhp://Input, otherwise the situation will be. The length of the fill, specified by Coentent-length.4OnlyContent-TypeForApplication/X-Www-Data-urlencodedWhenPhp://Input data is consistent with $_post data.5,Php://Input data is always the same as $http_raw_post_data, but php://input is more efficient than $http_raw_post_data and does not require special settings php.ini6,PhpWill bePATHof the fieldQuery_pathsection, fill in global variables$_get。 Typically,GETMethod commits thehttpRequestBodyis empty. Example1.PhpUseFile_get_contents("Php://input")Or$HTTP _raw_post_datacan receiveXmlData such as:GetXML.Php;//Receive XML Address 
                                                                                                                                                  Php$xmldata=File_get_contents("Php://input");$data=(Array)Simplexml_load_string($xmldata);?>Here's$Datais the inclusionXmlAn array of data, specificallyPhpAnalyticalXmlDetailed method of data updateSendXML.Php 
                                                                                                                                                                                     Php$xml='
                                                                                                                                                                                         
                                                                                                                                                                                          
                                                                                                                                                                                           XMLData
                                                                                                                                                                                          
                                                                                                                                                                                         ';//The XML to send$url='http://localhost/test/getXML.php';//Receive XML Address$header='Content-type:text/xml';//Define Content-type as XML$ch=Curl_init();//Initialize Curlcurl_setopt($ch,Curlopt_url,$url);//Set up linkscurl_setopt($ch,Curlopt_returntransfer,1);//Set whether to return informationcurl_setopt($ch,Curlopt_httpheader,$header);//Set HTTP Headerscurl_setopt($ch,Curlopt_post,1);//Set to post modecurl_setopt($ch,Curlopt_postfields,$Xml);//Post data$response=Curl_exec($ch);//Receive return informationIf(Curl_errno($ch)){//Error message is displayedPrintCurl_error($ch);}Curl_close($ch);//Turn off Curl LinksEcho$response;//Show return information?>2A mobile phone upload image to the server's applet upload file
                                                                                                                                                                                                                                                                                                                      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}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;}?>Receiving files
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Php/** *recieve ImageData **/Error_reporting(E_all);functionGet_contents(){$xmlstr=file_get_contents(the PHP://input"); $filename= Time().'. PNG'; if(File_put_contents($filename,$StrXml)){ Echo'Success'; }Else{ Echo'failed'; } } get_contents(); ?>3. GetHTTPRequest Original/** * Get HTTP request original *@returnstring*/ functionGet_http_raw() { $raw = ''; //(1) Request Line$raw .= $_server['Request_method'].' '.$_server['Request_uri'].' '.$_server['Server_protocol']."\ r\ n"; //(2) Request headersforeach($_server as$key=$value) { if(substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); $key = Str_replace('_', '-', $key); $raw .= $key.': '.$value."\ r\ n"; } } //(3) blank line$raw .= "\ r\ n"; //(4) Request Body$raw .= file_get_contents(' PHP://input'); return$raw; }

The above describes another way to use file_get_contents (), including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.