程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php獲取post中的json數據的實現方法

php獲取post中的json數據的實現方法

編輯:PHP綜合
突然想到了以前接觸過flash將圖片二進制流傳給php,靈機一動用$GLOBALS['HTTP_RAW_POST_DATA']獲取到了。
於是就深入的查了一下,原來PHP默認只識別application/x-www.form-urlencoded標准的數據類型,因此,對型如text/xml 或者 soap 或者 application/octet-stream 之類的內容無法解析,如果用$_POST數組來接收就會失敗!故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來接收。

php的HTTP_RAW_POST_DATA
用Content-Type=text/xml 類型,提交一個xml文檔內容給了php server,要怎麼獲得這個POST數據。
The RAW / uninterpreted HTTP POST information can be accessed with: $GLOBALS['HTTP_RAW_POST_DATA'] This is useful in cases where the post Content-Type is not something PHP understands (such as text/xml).
由於PHP默認只識別application/x-www.form-urlencoded標准的數據類型,因此,對型如text/xml的內容無法解析為$_POST數組,故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來接收。
另外還有一項 php://input 也可以實現此這個功能
php://input 允許讀取 POST 的原始數據。和 $HTTP_RAW_POST_DATA 比起來,它給內存帶來的壓力較小,並且不需要任何特殊的 php.ini 設置。php://input 不能用於 enctype="multipart/form-data"。

應用

a.htm
復制代碼 代碼如下:
<form action="post.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit">
</form>

post.php
復制代碼 代碼如下:
<? echo file_get_contents("php://input");?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved