PHP輸入資料流php://input執行個體講解_php技巧

來源:互聯網
上載者:User

對於php://input介紹,PHP官方手冊文檔有一段話對它進行了很明確地概述。
“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”.

翻譯過來,是這樣:
“php://input可以讀取沒有處理過的POST資料。相較於$HTTP_RAW_POST_DATA而言,它給記憶體帶來的壓力較小,並且不需要特 殊的php.ini設定。php://input不能用於enctype=multipart/form-data”
總結如下:

  • 1)、Coentent-Type僅在取值為application/x-www-data-urlencoded和multipart/form-data兩種情況下,PHP才會將http請求資料包中相應的資料填入全域變數$_POST
  • 2)、PHP不能識別的Content-Type類型的時候,會將http請求包中相應的資料填入變數$HTTP_RAW_POST_DATA
  • 3)、只有Coentent-Type為multipart/form-data的時候,PHP不會將http請求資料包中的相應資料填入php://input,否則其它情況都會。填入的長度,由Coentent-Length指定。
  • 4)、只有Content-Type為application/x-www-data-urlencoded時,php://input資料才跟$_POST資料相一致。
  • 5)、php://input資料總是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更湊效,且不需要特殊設定php.ini
  • 6)、PHP會將PATH欄位的query_path部分,填入全域變數$_GET。通常情況下,GET方法提交的http請求,body為空白。

總結起來就是,在用$_POST擷取不到由APP或者一些介面的回調資料時,就用php://input試試

1、 接受xml資料

//發送xml資料$xml = '<xml>xmldata</xml>';//要發送的xml $url = 'http://localhost/test/getXML.php';//接收XML地址 $header = 'Content-type: text/xml';//定義content-type為xml $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $url);//設定連結 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設定是否返回資訊 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設定HTTP頭 curl_setopt($ch, CURLOPT_POST, 1);//設定為POST方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST資料 $response = curl_exec($ch);//接收返回資訊 if(curl_errno($ch)){//出錯則顯示錯誤資訊 print curl_error($ch); } curl_close($ch); //關閉curl連結 echo $response;//顯示返回資訊 // php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml資料$xmldata = file_get_contents("php://input"); $data = (array)simplexml_load_string($xmldata);

2、手機上傳圖片到伺服器的小程式
發送

//@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; }

接收

/** *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:擷取HTTP請求原文

/** * 擷取HTTP請求原文 * @return string */function get_http_raw(){ $raw = ''; // (1) 請求行  $raw .= $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n"; // (2) 請求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) 空行  $raw .= "\r\n"; // (4) 請求Body  $raw .= file_get_contents('php://input'); return $raw;}

以上就是針對PHP輸入資料流舉的三個小栗子,目的在於協助大家更準確的理解PHP輸入資料流,希望大家有所收穫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.