亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

PHP輸入流php://input

瀏覽:99日期:2022-09-12 13:55:23

對于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”

總結如下:

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

總結起來就是,在用$_POST獲取不到由APP或者一些接口的回調數據時,就用php://input試試

Example1: 接受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);

Example2: 手機上傳圖片到服務器的小程序

發送

//@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.1rn'); fputs($fp, 'Host: {$host}rn'); fputs($fp, 'Content-Type: {$http_entity_type}rn'); fputs($fp, 'Content-Length: {$http_entity_length}rn'); fputs($fp, 'Connection: closernrn'); fputs($fp, $http_entity_body . 'rnrn'); 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();

Example3:獲取HTTP請求原文

/** * 獲取HTTP請求原文 * @return string */function get_http_raw(){ $raw = ’’; // (1) 請求行 $raw .= $_SERVER[’REQUEST_METHOD’] . ’ ’ . $_SERVER[’REQUEST_URI’] . ’ ’ . $_SERVER[’SERVER_PROTOCOL’] . 'rn'; // (2) 請求Headers foreach ($_SERVER as $key => $value) { if (substr($key , 0 , 5) === ’HTTP_’) { $key = substr($key , 5); $key = str_replace(’_’ , ’-’ , $key); $raw .= $key . ’: ’ . $value . 'rn'; } } // (3) 空行 $raw .= 'rn'; // (4) 請求Body $raw .= file_get_contents(’php://input’); return $raw;}

標簽: PHP
相關文章:
主站蜘蛛池模板: 2015xxx小明永久免费 | 日韩中文字幕在线观看视频 | 99re8免费视频精品全部 | 乱爱性全过程免费视频 | 鲁丝丝国产一区二区 | 最近中国免费高清观看 | 亚洲欧美日韩在线不卡 | 亚洲最大的黄色网 | 国产尤物视频在线 | 看亚洲a级一级毛片 | 一区二区三区四区视频在线 | 欧美特级黄色片 | 亚洲高清免费观看 | 亚洲综合丁香 | 91蝌蚪国产 | 自拍 第一页 | 欧美一级在线 | www在线小视频免费 www在线观看免费视频 | 99re最新地址 | 国产免费一级高清淫日本片 | 国产三级图片 | 99久久精品国产综合一区 | 欧美成人看片黄a免费 | 晚上睡不着偷偷看b站免费是视频 | 国产草草 | 精品玖玖玖视频在线观看 | 毛片站 | 欧美性生活视频免费播放网址大全观看 | 青青草国产成人久久91网 | 午夜男人女人爽爽爽视频 | 久久久久草| 在线碰 | 久久机热这里只有精品无需 | 国产青草视频在线观看 | 国产婷婷一区二区在线观看 | 国产成人精品999在线观看 | 一区二区三区杨幂在线观看 | 免费观看国产精品 | a级黄色毛片三个搞一 | 日本a一级毛片免费观看 | 亚洲视频不卡 |