("php://input")的使用方法包含HTTP請(qǐng)求內(nèi)容發(fā)送

$data = file_get_contents("php://input");

php://input 是個(gè)可以訪問請(qǐng)求的原始數(shù)據(jù)的只讀流。 POST 請(qǐng)求的情況下,最好使用 php://input 來代替 $HTTP_RAW_POST_DATA,因?yàn)樗灰蕾囉谔囟ǖ?php.ini 指令。 而且,這樣的情況下 $HTTP_RAW_POST_DATA 默認(rèn)沒有填充, 比激活 always_populate_raw_post_data 潛在需要更少的內(nèi)存。 enctype="multipart/form-data" 的時(shí)候 php://input 是無效的。 

1, php://input 可以讀取http entity body中指定長(zhǎng)度的值,由Content-Length指定長(zhǎng)度,不管是POST方式或者GET方法提交過來的數(shù)據(jù)。但是,一般GET方法提交數(shù)據(jù) 時(shí),http request entity body部分都為空。
2,php://input 與$HTTP_RAW_POST_DATA讀取的數(shù)據(jù)是一樣的,都只讀取Content-Type不為multipart/form-data的數(shù)據(jù)。

1,Content-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為空。

例子
1.php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml數(shù)據(jù)
比如:
getXML.php;//接收XML地址

<?php
$xmldata = file_get_contents("php://input");
$data = (array)simplexml_load_string($xmldata); //XML時(shí)所需的 進(jìn)行安全處理
?>

這里的$data就是包含xml數(shù)據(jù)的數(shù)組,具體php解析xml數(shù)據(jù)更新詳細(xì)的方法
sendXML.php

<?php
$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;//顯示返回信息 

?>

2.一個(gè)手機(jī)上傳圖片到服務(wù)器的小程序
上傳文件
<?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); //4096  避免瀏覽器忽略信息
    } 
    fclose($fp); 
    echo $d; 
 } 

?>

接收文件
<?php
/**
*Recieve image data
**/
error_reporting(E_ALL);

 function get_contents() {    
    $xmlstr= file_get_contents("php://input"); 
    $filename=time().'.png'; 
    if(file_put_contents($filename,$xmlstr)){ 
     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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容