$data = file_get_contents("php://input");
php://input 是個可以訪問請求的未經處理資料的唯讀流。 POST 請求的情況下,最好使用 php://input 來代替 $HTTP_RAW_POST_DATA,因為它不依賴於特定的 php.ini 指令。 而且,這樣的情況下 $HTTP_RAW_POST_DATA 預設沒有填充, 比啟用 always_populate_raw_post_data 潛在需要更少的記憶體。 enctype="multipart/form-data" 的時候 php://input 是無效的。
1, php://input 可以讀取http entity body中指定長度的值,由Content-Length指定長度,不管是POST方式或者GET方法提交過來的資料。但是,一般GET方法提交資料 時,http request entity body部分都為空白。
2,php://input 與$HTTP_RAW_POST_DATA讀取的資料是一樣的,都唯讀取Content-Type不為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為空白。
例子
1.php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml資料
比如:
getXML.php;//接收XML地址
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!--?php $xml = '<xml-->xmldata' ; //要發送的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 ; //顯示返回資訊 ?> |
2.一個手機上傳圖片到伺服器的小程式
上傳檔案
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!--?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); } fclose( $fp ); echo $d ; } ?--> |
接收檔案
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!--?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請求原文
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * 擷取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 |