Rumah > pembangunan bahagian belakang > tutorial php > 微信公众号开发---微信请求服务端取值问题

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

WBOY
Lepaskan: 2016-07-29 09:12:06
asal
1332 orang telah melayarinya

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

<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>}
    }
Salin selepas log masuk

以上是处理报文的部分代码

<span>$requestStr = $_REQUEST['mpxml']; </span>
Salin selepas log masuk
这个不用说了,接受name为mpxml的值

但是在服务端代码上线之后,这样写应该是接受不到微信发送服务端的请求报文(xml格式)的。

<span>$requestStr </span>= <span>$GLOBALS</span>[<span>"HTTP_RAW_POST_DATA"</span>]<span>;</span><span>//上线放开</span>
Salin selepas log masuk
或者
<span>$requestStr </span>= file_get_contents(<span>"php://input"</span>)<span>;</span><span>//上线放开</span>
Salin selepas log masuk
都可以接受到微信的请求报文,推荐第二种方法,它给内存带来的压力较小,并且不需要任何特殊的php.int设置

以上就介绍了微信公众号开发---微信请求服务端取值问题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan