PHP輸入流php://input實(shí)例講解
對(duì)于php://input介紹,PHP官方手冊(cè)文檔有一段話對(duì)它進(jìn)行了很明確地概述。
“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”.
翻譯過(guò)來(lái),是這樣:
“php://input可以讀取沒(méi)有處理過(guò)的POST數(shù)據(jù)。相較于$HTTP_RAW_POST_DATA而言,它給內(nèi)存帶來(lái)的壓力較小,并且不需要特 殊的php.ini設(shè)置。php://input不能用于enctype=multipart/form-data”
總結(jié)如下:
- 1)、Coentent-Type僅在取值為application/x-www-data-urlencoded和multipart/form-data兩種情況下,PHP才會(huì)將http請(qǐng)求數(shù)據(jù)包中相應(yīng)的數(shù)據(jù)填入全局變量$_POST
- 2)、PHP不能識(shí)別的Content-Type類型的時(shí)候,會(huì)將http請(qǐng)求包中相應(yīng)的數(shù)據(jù)填入變量$HTTP_RAW_POST_DATA
- 3)、只有Coentent-Type為multipart/form-data的時(shí)候,PHP不會(huì)將http請(qǐng)求數(shù)據(jù)包中的相應(yīng)數(shù)據(jù)填入php://input,否則其它情況都會(huì)。填入的長(zhǎng)度,由Coentent-Length指定。
- 4)、只有Content-Type為application/x-www-data-urlencoded時(shí),php://input數(shù)據(jù)才跟$_POST數(shù)據(jù)相一致。
- 5)、php://input數(shù)據(jù)總是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更湊效,且不需要特殊設(shè)置php.ini
- 6)、PHP會(huì)將PATH字段的query_path部分,填入全局變量$_GET。通常情況下,GET方法提交的http請(qǐng)求,body為空。
總結(jié)起來(lái)就是,在用$_POST獲取不到由APP或者一些接口的回調(diào)數(shù)據(jù)時(shí),就用php://input試試
1、 接受xml數(shù)據(jù)
//發(fā)送xml數(shù)據(jù) $xml = '<xml>xmldata</xml>';//要發(fā)送的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);//設(shè)置鏈接 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設(shè)置是否返回信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設(shè)置HTTP頭 curl_setopt($ch, CURLOPT_POST, 1);//設(shè)置為POST方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST數(shù)據(jù) $response = curl_exec($ch);//接收返回信息 if(curl_errno($ch)){//出錯(cuò)則顯示錯(cuò)誤信息 print curl_error($ch); } curl_close($ch); //關(guān)閉curl鏈接 echo $response;//顯示返回信息 // php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml數(shù)據(jù) $xmldata = file_get_contents("php://input"); $data = (array)simplexml_load_string($xmldata);
2、手機(jī)上傳圖片到服務(wù)器的小程序
發(fā)送
//@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請(qǐng)求原文
/** * 獲取HTTP請(qǐng)求原文 * @return string */ function get_http_raw(){ $raw = ''; // (1) 請(qǐng)求行 $raw .= $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n"; // (2) 請(qǐng)求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) 請(qǐng)求Body $raw .= file_get_contents('php://input'); return $raw; }
以上就是針對(duì)PHP輸入流舉的三個(gè)小栗子,目的在于幫助大家更準(zhǔn)確的理解PHP輸入流,希望大家有所收獲。
相關(guān)文章
php中簡(jiǎn)單的對(duì)稱加密算法實(shí)現(xiàn)
最近突發(fā)奇想要往數(shù)據(jù)庫(kù)里保存一些機(jī)密的東西,然后就想著怎么讓別人即使進(jìn)入到了數(shù)據(jù)庫(kù)也看不懂存儲(chǔ)的是什么,那么只有加密了;可是我們自己還要看呢,那只能找一些對(duì)稱加密的算法了,我們想看的時(shí)候再解密回來(lái)。下面就介紹了php中簡(jiǎn)單的對(duì)稱加密算法實(shí)現(xiàn)。2017-01-01PHP獲取兩個(gè)日期之間的所有日期的實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了一個(gè)獲取日期示例,用于計(jì)算給定開(kāi)始和結(jié)束日期之間的所有日期,文中有相關(guān)的代碼示例,感興趣的小伙伴可以自己動(dòng)手嘗試一下2023-08-08PHP實(shí)現(xiàn)的XXTEA加密解密算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的XXTEA加密解密算法,結(jié)合實(shí)例形式分析了php XXTEA加密解密算法類的定義與簡(jiǎn)單使用操作技巧,需要的朋友可以參考下2018-08-08php+mysqli批量查詢多張表數(shù)據(jù)的方法
這篇文章主要介紹了php+mysqli批量查詢多張表數(shù)據(jù)的方法,涉及multi_query、store_result及more_results等函數(shù)的使用技巧,需要的朋友可以參考下2015-01-01Zend studio for eclipse中使php可以調(diào)用mysql相關(guān)函數(shù)的設(shè)置方法
默認(rèn)情況zend studio 中的php是不支持mysql 相關(guān)操作,但通過(guò)下面的方法即可解決2008-10-10yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證詳解
這篇文章主要給大家介紹了關(guān)于yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05