Home >Backend Development >PHP Tutorial >WeChat public account development---WeChat request server value problem

WeChat public account development---WeChat request server value problem

WBOY
WBOYOriginal
2016-07-29 09:12:061335browse

I recently developed a WeChat official account and learned some new things. I will record them here and hope to help others

<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>}
    }

The above is part of the code for processing messages

<span>$requestStr = $_REQUEST['mpxml']; </span>
Needless to say, accept the value of name as mpxml

But after the server code is online, if you write it like this, you should not be able to accept the request message (xml format) sent by WeChat from the server. Both

<span>$requestStr </span>= <span>$GLOBALS</span>[<span>"HTTP_RAW_POST_DATA"</span>]<span>;</span><span>//上线放开</span>
or
<span>$requestStr </span>= file_get_contents(<span>"php://input"</span>)<span>;</span><span>//上线放开</span>
can receive request messages from WeChat. The second method is recommended. It puts less pressure on memory and does not require any special php.int settings.

The above has introduced the development of WeChat public accounts---the issue of value acquisition on the WeChat request server, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn