欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP中為什么使用file_get_contents("php://input")接收微信通知

 更新時(shí)間:2023年07月01日 10:36:56   投稿:yin  
微信用戶(hù)和公眾號(hào)產(chǎn)生交互的過(guò)程中,用戶(hù)的某些操作會(huì)使得微信服務(wù)器事件推送,通知到開(kāi)發(fā)者中心設(shè)置的服務(wù)器地址(回調(diào)url),開(kāi)發(fā)者可以獲取到該信息。PHP中為什么使用file_get_contents("php://input")來(lái)接收呢?為什么有些場(chǎng)景php://input接收不到?

微信用戶(hù)和公眾號(hào)產(chǎn)生交互的過(guò)程中,用戶(hù)的某些操作會(huì)使得微信服務(wù)器通過(guò)事件推送的形式,通知到開(kāi)發(fā)者在開(kāi)發(fā)者中心處設(shè)置的服務(wù)器地址(回調(diào)url),從而開(kāi)發(fā)者可以獲取到該信息。PHP中為什么會(huì)使用file_get_contents("php://input")來(lái)接收呢?為什么有些場(chǎng)景file_get_contents("php://input")會(huì)接收不到呢?

php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml數(shù)據(jù),

file_get_contents()

file_get_contents() 函數(shù)把整個(gè)文件讀入一個(gè)字符串中。

php://input

php://input 是個(gè)可以訪問(wèn)請(qǐng)求的原始數(shù)據(jù)的只讀流。 POST 請(qǐng)求的情況下,最好使用 php://input 來(lái)代替 $HTTP_RAW_POST_DATA,因?yàn)樗灰蕾?lài)于特定的 php.ini 指令。 而且,這樣的情況下 $HTTP_RAW_POST_DATA 默認(rèn)沒(méi)有填充, 比激活 always_populate_raw_post_data 潛在需要更少的內(nèi)存。 enctype="multipart/form-data" 的時(shí)候 php://input 是無(wú)效的。

php://input使用范圍

1、 讀取POST數(shù)據(jù)

2、不能用于multipart/form-data類(lèi)型

$http_raw_post_data

$http_raw_post_data是PHP內(nèi)置的一個(gè)全局變量。它用于,PHP在無(wú)法識(shí)別的Content-Type的情況下,將POST過(guò)來(lái)的數(shù)據(jù)原樣地填入變量$http_raw_post_data。它同樣無(wú)法讀取Content-Type為multipart/form-data的POST數(shù)據(jù)。需要設(shè)置php.ini中的always_populate_raw_post_data值為On,PHP才會(huì)總把POST數(shù)據(jù)填入變量$http_raw_post_data。

php://input、$http_raw_post_data、$_POST、$_GET區(qū)別

1、GET提交時(shí),不會(huì)指定Content-Type和Content-Length,它表示http請(qǐng)求body中的數(shù)據(jù)是使用http的post方法提交的表單數(shù)據(jù),并且進(jìn)行了urlencode()處理。

2、 POST提交時(shí),Content- Type取值為application/x-www-form-urlencoded時(shí),也指明了Content-Length的值,php會(huì)將http請(qǐng)求body相應(yīng)數(shù)據(jù)會(huì)填入到數(shù) 組$_POST,填入到$_POST數(shù)組中的數(shù)據(jù)是進(jìn)行urldecode()解析的結(jié)果。

3、php://input數(shù)據(jù),只要Content-Type不為 multipart/form-data(該條件限制稍后會(huì)介紹)。那么php://input數(shù)據(jù)與http entity body部分?jǐn)?shù)據(jù)是一致的。該部分相一致的數(shù)據(jù)的長(zhǎng)度由Content-Length指定。 
4、僅當(dāng)Content-Type為application/x-www-form-urlencoded且提交方法是POST方法時(shí),$_POST數(shù)據(jù)與php://input數(shù)據(jù)才是”一致”(打上引號(hào),表示它們格式不一致,內(nèi)容一致)的。其它情況,它們都不一致。 
5、php://input讀取不到$_GET數(shù)據(jù)。是因?yàn)?_GET數(shù)據(jù)作為query_path寫(xiě)在http請(qǐng)求頭部(header)的PATH字段,而不是寫(xiě)在http請(qǐng)求的body部分。

接收和發(fā)送XML的php示例

php示例:接收XML

接收xml數(shù)據(jù),并轉(zhuǎn)化為array數(shù)組。

<?php
$xmlData = file_get_contents("php://input");
$obj = simplexml_load_string($xmlData,"SimpleXMLElement", LIBXML_NOCDATA);
$json = json_decode(json_encode($obj),true);

php示例:發(fā)送xml

<?php
$xml = '<xml>xmldata</xml>';//要發(fā)送的xml
$url = 'http://***.php';//接收XML地址
$header = 'Content-type: text/xml';//定義content-type為xml
$ch = curl_init(); //初始化
curl curl_setopt($ch, CURLOPT_URL, $url);//設(shè)置鏈接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設(shè)置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設(shè)置HTTP頭
curl_setopt($ch, CURLOPT_POST, 1);//設(shè)置為POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST數(shù)據(jù)
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出錯(cuò)則顯示錯(cuò)誤信息
    print curl_error($ch);
}
curl_close($ch); //關(guān)閉curl鏈接
echo $response;//顯示返回信息
?>

到此這篇關(guān)于PHP中為什么使用file_get_contents("php://input")接收微信通知的文章就介紹到這了,更多相關(guān)PHP中file_get_contents("php://input")接收微信通知內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論