首頁 > 後端開發 > php教程 > 微信公眾號開發---微信請求服務端取值問題

微信公眾號開發---微信請求服務端取值問題

WBOY
發布: 2016-07-29 09:12:06
原創
1332 人瀏覽過

最近開發了微信公眾號,學了一些新東西,在這記錄一下,也希望能幫助到別人

<span>public function </span><span>responseMsg</span>()
    {
<span>//        $requestStr = $_REQUEST['mpxml'];  //线下测试放开
</span><span>$requestStr </span>= <span>$GLOBALS</span>[<span>"HTTP_RAW_POST_DATA"</span>]<span>;</span><span>//上线放开
</span><span>if</span>(<span>$requestStr</span>==<span>''</span>)<span>$requestStr </span>= file_get_contents(<span>"php://input"</span>)<span>;</span><span>//上线放开
</span><span>$this</span>-><span>Log</span>-><span>output</span>(<span>'req'</span><span>, </span><span>"请求报文" </span>. <span>$requestStr</span>)<span>;
</span><span>if </span>(!<span>empty</span>(<span>$requestStr</span>)) {
            <span>$postObj </span>= simplexml_load_string(<span>$requestStr</span><span>, </span><span>'SimpleXMLElement'</span><span>, </span><span><em>LIBXML_NOCDATA</em></span>)<span>;
</span><span>$fromUsername </span>= <span>$postObj</span>-><span>FromUserName</span><span>;
</span><span>$toUsername </span>= <span>$postObj</span>-><span>ToUserName</span><span>;
</span><span>$msgType </span>= <span>$postObj</span>-><span>MsgType</span><span>;
</span><span>$keyword </span>= trim(<span>$postObj</span>-><span>Content</span>)<span>;
</span><span>$arr </span>= <span>array</span>(<span>'fromUsername' </span>=> <span>$fromUsername</span><span>, </span><span>'toUsername' </span>=> <span>$toUsername</span><span>, </span><span>'msgType' </span>=> <span>$msgType</span><span>,
</span><span>'keyword' </span>=> <span>$keyword</span><span>, </span><span>'postObj' </span>=> <span>$postObj</span>)<span>;
</span><span>$responseStr </span>= <span>$this</span>-><span>controllers</span>-><span>index</span>(<span>$arr</span>)<span>;
</span><span>$this</span>-><span>Log</span>-><span>output</span>(<span>'res'</span><span>, </span><span>"应答报文" </span>. <span>$responseStr</span>)<span>;
</span><span>echo </span><span>$responseStr</span><span>;
</span>} <span>else </span>{
            <span>echo </span><span>''</span><span>;
</span><span>exit</span><span>;
</span>}
    }
登入後複製

以上是處理報文的部分代碼

<span>$requestStr = $_REQUEST['mpxml']; </span>
登入後複製
這個不用說了,接受name為mpxml的值

但是在服務端程式碼上線之後,這樣寫應該是接受不到微信發送服務端的請求封包(xml格式)的。

<span>$requestStr </span>= <span>$GLOBALS</span>[<span>"HTTP_RAW_POST_DATA"</span>]<span>;</span><span>//上线放开</span>
登入後複製

<span>$requestStr </span>= file_get_contents(<span>"php://input"</span>)<span>;</span><span>//上线放开</span>
登入後複製
都可以接受到微信的請求報文,推薦第二種方法,它給內存帶來的壓力較小,並且不需要任何特殊的php.int設置

以上就介紹了微信公眾號開發---微信請求服務端取值問題,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板