首頁 > 後端開發 > php教程 > 微信公众平台开发(三) 订阅事件(subscribe)处理_PHP教程

微信公众平台开发(三) 订阅事件(subscribe)处理_PHP教程

WBOY
發布: 2016-07-20 11:13:41
原創
1144 人瀏覽過

新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,默认代码中没有对这一事件进行相应回复处理。

在新用户关注公众平台后,可能想知道该平台提供了哪些功能,以及怎样使用该平台,通俗一点讲就是该平台的“使用说明书”。

本文将详细讲述对subscribe事件的处理过程,回复相应信息提升交互性。

微信目前提供了五种消息类型,分别为:

  • 文本消息(text);
  • 图片消息(image);
  • 地理位置消息(location);
  • 链接消息(link);
  • 事件推送(event);

接收到消息后,首先需要对消息类型做出判断,然后再针对不同类型的消息做出处理。在事件推送中,事件类型又分为三种,subscribe(订阅)、unsubscribe(取消订阅)、CLICK(自定义菜单点击事件),还需要再加一次判断;判断为subscribe事件后,根据设定好的欢迎消息,回复给用户。

<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>
登入後複製

说明:

$RX_TYPE = trim($postObj->MsgType);   得到消息类型;

case "text":
  $resultStr = $this->handleText($postObj);   使用handleText() 函数处理文本消息;

case "event":
  $resultStr = $this->handleEvent($postObj);   使用handleEvent() 函数处理事件推送;

<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>
登入後複製

说明:

如果是subscribe事件,设定回复内容为“感谢您关注【卓锦苏州】...”;

<?<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 $contentStr</span> = "Welcome to wechat world!"<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/440365.htmlTechArticle新用户关注微信公众平台,将产生一个订阅事件,即 subscribe事件 ,默认代码中没有对这一事件进行相应回复处理。 在新用户关注公众平台...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板