php獲取post中的json數(shù)據(jù)的實(shí)現(xiàn)方法
更新時(shí)間:2011年06月08日 23:13:37 作者:
最近用到ext與PHP交互,ext把json數(shù)據(jù)post給PHP,但在PHP里面$_post獲取不到,$_REQUEST也獲取不到,但是通過(guò)firedebug看到的請(qǐng)求信息確實(shí)是把JSON數(shù)據(jù)post給了PHP,這什么情況
突然想到了以前接觸過(guò)flash將圖片二進(jìn)制流傳給php,靈機(jī)一動(dòng)用$GLOBALS['HTTP_RAW_POST_DATA']獲取到了。
于是就深入的查了一下,原來(lái)PHP默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型,因此,對(duì)型如text/xml 或者 soap 或者 application/octet-stream 之類的內(nèi)容無(wú)法解析,如果用$_POST數(shù)組來(lái)接收就會(huì)失??!故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來(lái)接收。
php的HTTP_RAW_POST_DATA
用Content-Type=text/xml 類型,提交一個(gè)xml文檔內(nèi)容給了php server,要怎么獲得這個(gè)POST數(shù)據(jù)。
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默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型,因此,對(duì)型如text/xml的內(nèi)容無(wú)法解析為$_POST數(shù)組,故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來(lái)接收。
另外還有一項(xiàng) php://input 也可以實(shí)現(xiàn)此這個(gè)功能
php://input 允許讀取 POST 的原始數(shù)據(jù)。和 $HTTP_RAW_POST_DATA 比起來(lái),它給內(nèi)存帶來(lái)的壓力較小,并且不需要任何特殊的 php.ini 設(shè)置。php://input 不能用于 enctype="multipart/form-data"。
應(yīng)用
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");?>
于是就深入的查了一下,原來(lái)PHP默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型,因此,對(duì)型如text/xml 或者 soap 或者 application/octet-stream 之類的內(nèi)容無(wú)法解析,如果用$_POST數(shù)組來(lái)接收就會(huì)失??!故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來(lái)接收。
php的HTTP_RAW_POST_DATA
用Content-Type=text/xml 類型,提交一個(gè)xml文檔內(nèi)容給了php server,要怎么獲得這個(gè)POST數(shù)據(jù)。
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默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型,因此,對(duì)型如text/xml的內(nèi)容無(wú)法解析為$_POST數(shù)組,故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來(lái)接收。
另外還有一項(xiàng) php://input 也可以實(shí)現(xiàn)此這個(gè)功能
php://input 允許讀取 POST 的原始數(shù)據(jù)。和 $HTTP_RAW_POST_DATA 比起來(lái),它給內(nèi)存帶來(lái)的壓力較小,并且不需要任何特殊的 php.ini 設(shè)置。php://input 不能用于 enctype="multipart/form-data"。
應(yīng)用
a.htm
復(fù)制代碼 代碼如下:
<form action="post.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit">
</form>
post.php
復(fù)制代碼 代碼如下:
<? echo file_get_contents("php://input");?>
您可能感興趣的文章:
- PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
- PHP下使用CURL方式POST數(shù)據(jù)至API接口的代碼
- PHP使用curl模擬post上傳及接收文件的方法
- php的curl實(shí)現(xiàn)get和post的代碼
- php curl模擬post提交數(shù)據(jù)示例
- PHP使用curl函數(shù)發(fā)送Post請(qǐng)求的注意事項(xiàng)
- PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求)
- PHP CURL模擬GET及POST函數(shù)代碼
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- 使用PHP接收POST數(shù)據(jù),解析json數(shù)據(jù)
- PHP基于curl模擬post提交json數(shù)據(jù)示例
相關(guān)文章
PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能示例
這篇文章主要介紹了PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能,涉及php針對(duì)頁(yè)面img元素的正則匹配與替換操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
PHP學(xué)習(xí)筆記(三):數(shù)據(jù)類型轉(zhuǎn)換與常量介紹
這篇文章主要介紹了PHP學(xué)習(xí)筆記(三):數(shù)據(jù)類型轉(zhuǎn)換與常量介紹,本文講解了PHP數(shù)據(jù)類型相互轉(zhuǎn)換、常量的聲明與使用等內(nèi)容,需要的朋友可以參考下2015-04-04
PHP自定義函數(shù)格式化json數(shù)據(jù)示例
這篇文章主要介紹了PHP自定義函數(shù)格式化json數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了php通過(guò)自定義函數(shù)進(jìn)行字符串與數(shù)組的遍歷、轉(zhuǎn)換、計(jì)算等操作實(shí)現(xiàn)json數(shù)據(jù)格式化的功能,需要的朋友可以參考下2016-09-09
PHP加密函數(shù) Javascript/Js 解密函數(shù)
php加密,js解密,貌似沒(méi)什么意義,主要是key在js中會(huì)被看到,不過(guò)在某些地方可能會(huì)用到2013-09-09

