首页 >后端开发 >php教程 >微信公众号开发---微信请求服务端取值问题

微信公众号开发---微信请求服务端取值问题

WBOY
WBOY原创
2016-07-29 09:12:061332浏览

最近开发了微信公众号,学了一些新东西,在这记录一下,也希望能帮助到别人

<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教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn