微信公共平台开发接口(二)

AndyYang| 阅读:1391 发表时间:2014-04-27 12:39:46 微信平台
摘要:是不是在关注了有些订阅号或者服务号之后,马上会收到一条消息。什么回复1,怎样怎样;回复2,怎样怎样之类的。

是不是在关注了有些订阅号或者服务号之后,马上会收到一条消息。什么回复1,怎样怎样;回复2,怎样怎样之类的。

拿我自己的博客举例,我的关注语是:
    感谢您关注AndyYang个人博客微信小助手。
    回复【1】返回两篇最新文章
    回复【2】返回两篇人气文章
    回复【3】返回两篇热评文章
    回复【4】返回两篇最新技术文章
    回复【5】返回两篇最新写作文章
    回复其他返回搜索关键字的两篇文章
    更多精彩内容,尽在:www.webyang.net。亲们,请多多支持哦,谢谢~

那这个怎么实现呢?
直接上代码:

<?php
/**
 * wechat php test
 */

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

class wechatCallbackapiTest
{

    public function valid()
    {
        $echoStr = $_GET["echostr"];

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

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

        //extract post data
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            $MsgType = $postObj->MsgType; //add
            $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
                </xml>";

            if($MsgType != 'event') {
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                }else{
                    echo "Input something...";
                }
            } else {
                $msgType = "text";
                $contentStr = "感谢您关注AndyYang个人博客微信小助手。\r\n".
                    "回复【1】返回两篇最新文章\r\n".
                    "回复【2】返回两篇人气文章\r\n".
                    "回复【3】返回两篇热评文章\r\n".
                    "回复【4】返回两篇最新技术文章\r\n".
                    "回复【5】返回两篇最新写作文章\r\n".
                    "回复其他返回搜索关键字的两篇文章\r\n".
                    "更多精彩内容,尽在:<a href='http://www.webyang.net'>www.webyang.net</a>。亲们,请多多支持哦,谢谢~";
                ;
            }
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;

        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING); //这个在新的sdk中添加了第二个参数(compare items as strings)
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

}

当然这里只是简单的实现下,在微信公共平台提供的sdk上做简单的修改,实际上msgtype类型很多,就算消息类型为event的,它里面也有subscribe、LOCATION等,而如果细化的话,就用Event为subscribe来处理初次关注的事件。

欢迎大家关注我的微信订阅号:webyangnet,或者扫我吧,亲!^_^

本文为AndyYang原创,转载请注明出处!
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

昵称:
邮箱:

  • 最新评论

iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号