PHP中php://input怎麼用

藏色散人
發布: 2023-03-14 14:50:01
原創
4003 人瀏覽過

php://input可以讀取沒有處理過的POST數據,其使用方法如“$xmldata = file_get_contents("php://input");”,用於接收xml資料。

PHP中php://input怎麼用

本文操作環境:Windows7系統,PHP7.1版,Dell G3電腦。

PHP中php://input怎麼用?

對於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 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 = &#39;<xml>xmldata</xml>&#39;;//要发送的xml 
$url = &#39;http://localhost/test/getXML.php&#39;;//接收XML地址 
$header = &#39;Content-type: text/xml&#39;;//定义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(&#39;btn.png&#39;); 
$http_entity_body = $data; 
$http_entity_type = &#39;application/x-www-form-urlencoded&#39;; 
$http_entity_length = strlen($http_entity_body); 
$host = &#39;127.0.0.1&#39;; 
$port = 80; 
$path = &#39;/image.php&#39;; 
$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().&#39;.png&#39;;
 if(($filename,$str)){
 echo &#39;success&#39;;
 }else{
 echo &#39;failed&#39;;
 } 
  }
get_contents();
登入後複製

3:取得HTTP請求原文

/**
 * 获取HTTP请求原文
 * @return string
 */
function get_http_raw(){
 $raw = &#39;&#39;;
 // (1) 请求行 
 $raw .= $_SERVER[&#39;REQUEST_METHOD&#39;] . &#39; &#39; . $_SERVER[&#39;REQUEST_URI&#39;] . &#39; &#39; . $_SERVER[&#39;SERVER_PROTOCOL&#39;] . "\r\n";
 // (2) 请求Headers 
 foreach ($_SERVER as $key => $value) {
 if (substr($key , 0 , 5) === &#39;HTTP_&#39;) {
  $key = substr($key , 5);
  $key = str_replace(&#39;_&#39; , &#39;-&#39; , $key);
  $raw .= $key . &#39;: &#39; . $value . "\r\n";
 }
 }
 // (3) 空行 
 $raw .= "\r\n";
 // (4) 请求Body 
 $raw .= file_get_contents(&#39;php://input&#39;);
 return $raw;
}
登入後複製

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

推薦學習:《PHP影片教學

以上是PHP中php://input怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!