最近開發了微信公眾號,學了一些新東西,在這記錄一下,也希望能幫助到別人
<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>
但是在服務端程式碼上線之後,這樣寫應該是接受不到微信發送服務端的請求封包(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教程有興趣的朋友有所幫助。