PHP中為什么使用file_get_contents("php://input")接收微信通知
微信用戶和公眾號(hào)產(chǎn)生交互的過程中,用戶的某些操作會(huì)使得微信服務(wù)器通過事件推送的形式,通知到開發(fā)者在開發(fā)者中心處設(shè)置的服務(wù)器地址(回調(diào)url),從而開發(fā)者可以獲取到該信息。PHP中為什么會(huì)使用file_get_contents("php://input")來接收呢?為什么有些場(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://inputphp://input 是個(gè)可以訪問請(qǐng)求的原始數(shù)據(jù)的只讀流。 POST 請(qǐng)求的情況下,最好使用 php://input 來代替 $HTTP_RAW_POST_DATA,因?yàn)樗灰蕾囉谔囟ǖ?php.ini 指令。 而且,這樣的情況下 $HTTP_RAW_POST_DATA 默認(rèn)沒有填充, 比激活 always_populate_raw_post_data 潛在需要更少的內(nèi)存。 enctype="multipart/form-data" 的時(shí)候 php://input 是無效的。
php://input使用范圍
1、 讀取POST數(shù)據(jù)
2、不能用于multipart/form-data類型
$http_raw_post_data$http_raw_post_data是PHP內(nèi)置的一個(gè)全局變量。它用于,PHP在無法識(shí)別的Content-Type的情況下,將POST過來的數(shù)據(jù)原樣地填入變量$http_raw_post_data。它同樣無法讀取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ù)的長度由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寫在http請(qǐng)求頭部(header)的PATH字段,而不是寫在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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
