久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:163日期:2022-06-13 17:26:57
目錄file_get_contents()php://input$http_raw_post_dataphp://input、$http_raw_post_data、$_POST、$_GET區(qū)別接收和發(fā)送XML的php示例php示例:接收XMLphp示例:發(fā)送xml

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

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

file_get_contents()

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

php://input

php://input 是個可以訪問請求的原始數(shù)據(jù)的只讀流。 POST 請求的情況下,最好使用 php://input 來代替 $HTTP_RAW_POST_DATA,因為它不依賴于特定的 php.ini 指令。 而且,這樣的情況下 $HTTP_RAW_POST_DATA 默認(rèn)沒有填充, 比激活 always_populate_raw_post_data 潛在需要更少的內(nèi)存。 enctype="multipart/form-data" 的時候 php://input 是無效的。

php://input使用范圍

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

2、不能用于multipart/form-data類型

$http_raw_post_data

$http_raw_post_data是PHP內(nèi)置的一個全局變量。它用于,PHP在無法識別的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才會總把POST數(shù)據(jù)填入變量$http_raw_post_data。

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

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

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

3、php://input數(shù)據(jù),只要Content-Type不為 multipart/form-data(該條件限制稍后會介紹)。那么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方法時,$_POST數(shù)據(jù)與php://input數(shù)據(jù)才是”一致”(打上引號,表示它們格式不一致,內(nèi)容一致)的。其它情況,它們都不一致。 5、php://input讀取不到$_GET數(shù)據(jù)。是因為$_GET數(shù)據(jù)作為query_path寫在http請求頭部(header)的PATH字段,而不是寫在http請求的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)){//出錯則顯示錯誤信息 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 云南省| 邯郸市| 三门县| 镇巴县| 丹东市| 洪泽县| 社会| 襄樊市| 益阳市| 彩票| 子长县| 论坛| 和龙市| 阿瓦提县| 富锦市| 盐城市| 斗六市| 民丰县| 措美县| 湖州市| 瓮安县| 绩溪县| 德江县| 泰来县| 滨海县| 英德市| 通城县| 天峻县| 阿瓦提县| 河曲县| 平江县| 巴青县| 黑龙江省| 弋阳县| 昭苏县| 广元市| 富锦市| 大渡口区| 昌图县| 汉川市| 淅川县|