Home > Backend Development > PHP Tutorial > WeChat public platform development (4) Simple reply function development_PHP tutorial

WeChat public platform development (4) Simple reply function development_PHP tutorial

WBOY
Release: 2016-07-20 11:12:44
Original
859 people have browsed it

The WeChat public platform can make judgments based on the information sent by users and then give corresponding replies, which has good interactivity. The following will simulate a simple reply function. Based on this case, developers can also basically understand the principles of WeChat interaction and conduct deeper development.

We can extract keywords from text messages sent by users through simple if...elseif...else....

The key code is as follows:

<span if</span>(<span $keyword</span>=="你好"<span ){
  </span><span $contentStr</span> = "hello"<span ;
}</span><span elseif</span>(<span $keyword</span>=="苏州"<span ){
  </span><span $contentStr</span> = "上有天堂,下有苏杭"<span ;
}</span><span else</span><span {
  </span><span $contentStr</span> = "感谢您关注【卓锦苏州】 微信号:zhuojinsz"<span ;
}</span>
Copy after login

If the user sends "Hello", it will reply "hello". If the user sends "Suzhou", it will reply "There is heaven above, there is Suzhou and Hangzhou below". For other information, it will reply with your welcome message.

<?<span php
</span><span /*</span><span *
  * wechat php test
  </span><span */</span>

<span //</span><span define your token</span>
<span define</span>("TOKEN", "zhuojin"<span );
</span><span $wechatObj</span> = <span new</span><span  wechatCallbackapiTest();
</span><span $wechatObj</span>-><span responseMsg();
</span><span //</span><span $wechatObj->valid();</span>

<span class</span><span  wechatCallbackapiTest
{
    </span><span /*</span><span public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }</span><span */</span>

    <span public</span> <span function</span><span  responseMsg()
    {
        </span><span //</span><span get post data, May be due to the different environments</span>
        <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ];

          </span><span //</span><span extract post data</span>
        <span if</span> (!<span empty</span>(<span $postStr</span><span )){
                
                  </span><span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span  LIBXML_NOCDATA);
                </span><span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType);

                </span><span switch</span>(<span $RX_TYPE</span><span )
                {
                    </span><span case</span> "text":
                        <span $resultStr</span> = <span $this</span>->handleText(<span $postObj</span><span );
                        </span><span break</span><span ;
                    </span><span case</span> "event":
                        <span $resultStr</span> = <span $this</span>->handleEvent(<span $postObj</span><span );
                        </span><span break</span><span ;
                    </span><span default</span>:
                        <span $resultStr</span> = "Unknow msg type: ".<span $RX_TYPE</span><span ;
                        </span><span break</span><span ;
                }
                </span><span echo</span> <span $resultStr</span><span ;
        }</span><span else</span><span  {
            </span><span echo</span> ""<span ;
            </span><span exit</span><span ;
        }
    }

    </span><span public</span> <span function</span> handleText(<span $postObj</span><span )
    {
        </span><span $fromUsername</span> = <span $postObj</span>-><span FromUserName;
        </span><span $toUsername</span> = <span $postObj</span>-><span ToUserName;
        </span><span $keyword</span> = <span trim</span>(<span $postObj</span>-><span Content);
        </span><span $time</span> = <span time</span><span ();
        </span><span $textTpl</span> = "<span <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml></span>"<span ;             
        </span><span if</span>(!<span empty</span>( <span $keyword</span><span  ))
        {
            </span><span $msgType</span> = "text"<span ;

            </span><span if</span>(<span $keyword</span>=="你好"<span ){
                </span><span $contentStr</span> = "hello"<span ;
            }</span><span elseif</span>(<span $keyword</span>=="苏州"<span ){
                </span><span $contentStr</span> = "上有天堂,下有苏杭"<span ;
            }</span><span else</span><span {
                </span><span $contentStr</span> = "感谢您关注【卓锦苏州】 微信号:zhuojinsz"<span ;
            }
            </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $fromUsername</span>, <span $toUsername</span>, <span $time</span>, <span $msgType</span>, <span $contentStr</span><span );
            </span><span echo</span> <span $resultStr</span><span ;
        }</span><span else</span><span {
            </span><span echo</span> "Input something..."<span ;
        }
    }

    </span><span public</span> <span function</span> handleEvent(<span $object</span><span )
    {
        </span><span $contentStr</span> = ""<span ;
        </span><span switch</span> (<span $object</span>-><span Event)
        {
            </span><span case</span> "subscribe":
                <span $contentStr</span> = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待..."<span ;
                </span><span break</span><span ;
            </span><span default</span> :
                <span $contentStr</span> = "Unknow Event: ".<span $object</span>-><span Event;
                </span><span break</span><span ;
        }
        </span><span $resultStr</span> = <span $this</span>->responseText(<span $object</span>, <span $contentStr</span><span );
        </span><span return</span> <span $resultStr</span><span ;
    }
    
    </span><span public</span> <span function</span> responseText(<span $object</span>, <span $content</span>, <span $flag</span>=0<span )
    {
        </span><span $textTpl</span> = "<span <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml></span>"<span ;
        </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span>, <span $flag</span><span );
        </span><span return</span> <span $resultStr</span><span ;
    }


    </span><span private</span> <span function</span><span  checkSignature()
    {
        </span><span $signature</span> = <span $_GET</span>["signature"<span ];
        </span><span $timestamp</span> = <span $_GET</span>["timestamp"<span ];
        </span><span $nonce</span> = <span $_GET</span>["nonce"<span ];    
                
        </span><span $token</span> =<span  TOKEN;
        </span><span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span );
        </span><span sort</span>(<span $tmpArr</span><span );
        </span><span $tmpStr</span> = <span implode</span>( <span $tmpArr</span><span  );
        </span><span $tmpStr</span> = <span sha1</span>( <span $tmpStr</span><span  );
        
        </span><span if</span>( <span $tmpStr</span> == <span $signature</span><span  ){
            </span><span return</span> <span true</span><span ;
        }</span><span else</span><span {
            </span><span return</span> <span false</span><span ;
        }
    }
}

</span>?>
Copy after login

Please follow Zhuojin Suzhou WeChat public account, Zhuojin Suzhou is developed based on SAE platform and is targeted at mainstream WeChat functions Conduct development testing.

You can follow the Zhuojin Suzhou public account to conduct functional testing and obtain new application development.

1. Log in to the WeChat client, friends -> Add friends -> Search number -> zhuojinsz, find and follow.

2. Scan the QR code:


We Believe, Great People Share Knowledge...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440427.htmlTechArticleThe WeChat public platform can judge based on the information sent by the user, and then give corresponding replies, which has good interactivity . The following will simulate a simple reply function. According to this case,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template