微信公众平台开发(四) 简单回复功能开发_PHP教程

WBOY
풀어 주다: 2016-07-20 11:12:44
원래의
844명이 탐색했습니다.

微信公众平台可以根据用户发送的信息进行判断,然后给出对应的回复,具有良好的交互性。下文将模拟简单的回复功能,根据这个案例,开发者也可以基本理解微信交互的原理,进行更深层次的开发。

用户发送过来的文本信息,我们可以提取关键字,通过简单的 if...elseif...else... 实现。

关键代码如下:

<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>
로그인 후 복사

如果用户发送"你好",则回复"hello",如果用户发送"苏州",则回复"上有天堂,下有苏杭",其他信息,则回复你的欢迎词。

<?<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>?>
로그인 후 복사

请关注 卓锦苏州 微信公众帐号,卓锦苏州 基于SAE 平台开发,针对于主流的微信功能进行开发测试。

您可以关注 卓锦苏州 公众帐号进行功能测试,以及获取新的应用开发。

1. 登录微信客户端,朋友们 -> 添加朋友 -> 搜号码 -> zhuojinsz,查找并关注。

2. 扫描二维码:

 


We Believe, Great People Share Knowledge...

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440427.htmlTechArticle微信公众平台可以根据用户发送的信息进行判断,然后给出对应的回复,具有良好的交互性。下文将模拟简单的回复功能,根据这个案例,...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿