PHP輸入資料流php://input

來源:互聯網
上載者:User

在使用xml-rpc的時候,server端擷取client資料,主要是通過php輸入資料流input,而不是$_POST數組。所以,這裡主要探討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 available with enctype=”multipart/form-data”. 

  翻譯過來,是這樣: 

  “php://input可以讀取沒有處理過的POST資料。相較於$HTTP_RAW_POST_DATA而言,它給記憶體帶來的壓力較小,並且不需要特殊的php.ini設定。php://input不能用於enctype=multipart/form-data”。 

  1,Content-Type取值為application/x-www-form-urlencoded時,php會將http請求body相應資料會填入到數組$_POST,填入到$_POST數組中的資料是進行urldecode()解析的結果。(其實,除了該Content-Type,還有multipart/form-data表示資料是表單資料,稍後我們介紹) 

  2,php://input資料,只要Content-Type不為multipart/form-data(該條件限制稍後會介紹)。那麼php://input資料與http entity body部分資料是一致的。該部分相一致的資料的長度由Content-Length指定。 

  3,僅當Content-Type為application/x-www-form-urlencoded且提交方法是POST方法時,$_POST資料與php://input資料才是”一致”(打上引號,表示它們格式不一致,內容一致)的。其它情況,它們都不一致。 

  4,php://input讀取不到$_GET資料。是因為$_GET資料作為query_path寫在http要求標頭部(header)的PATH欄位,而不是寫在http請求的body部分。 

  相信大家對php://input已經有一定深度地瞭解了。那麼$http_raw_post_data是什麼呢。$http_raw_post_data是PHP內建的一個全域變數。它用於,PHP在無法識別的Content-Type的情況下,將POST過來的資料原樣地填入變數$http_raw_post_data。它同樣無法讀取Content-Type為multipart/form-data的POST資料。需要設定php.ini中的always_populate_raw_post_data值為On,PHP才會總把POST資料填入變數$http_raw_post_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為空白。 

類比服務端程式如下: [php]  view plain copy <?php      //@file phpinput_server.php   $raw_post_data = file_get_contents('php://input', 'r');   echo "-------\<pre class="php" name="code">{1}</pre><br>   POST------------------\n";echo var_dump(<pre class="php" name="code">{1}</pre><br>   POST) . "\n";echo "-------php://input-------------\n";echo $raw_post_data . "\n";?>   <pre></pre>   <p><br>    </p>   <p>類比用戶端指令碼如下:</p>   <p> </p>   <pre class="php" name="code"><?php      //@file phpinput_post.php   $http_entity_body = 'n=' . urldecode('perfgeeks') . '&=' . urlencode('7788');   $http_entity_type = 'application/x-www-form-urlencoded';   $http_entity_length = strlen($http_entity_body);   $host = 'localhost';   $port = 80;   $path = '/ceshi/phpinput_server.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 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.