微信公众号之自动回复

微信公众号,智能回复!
1.首先,你需要一个域名!
2.然后需要进行配置!


若想配通,必须要TOKEN验证通过!

<?php
/**
 * Created by PhpStorm.
 * User: jiqing
 * Date: 18-7-19
 * Time: 下午6:30
 * 处理消息自动回复
 */

class AutoAction extends CommonAction
{
    public function index()
    {
        $timestamp = $_GET['timestamp'];//timestamp其实就是一个时间戳
        $nonce = $_GET['nonce'];//nonce是一个随机参数
        $token = "vTA2zJ3969BKJGSlJKYJhy22j96GKKlA";//这个token填写你在微信公众平台上写的那个值
        $signature = $_GET['signature'];//这个signature其实就是在微信公众平台已经加密好的字符串
        $echostr = $_GET['echostr'];
        $array = array($timestamp, $nonce, $token);
        sort($array);
        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);
        if ($tmpstr == $signature && $echostr) {
            echo $echostr;
            exit;
        } else {
            $this->reponseMsg();
        }
    }

    public function reponseMsg()
    {
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string($postArr);
        if (strtolower($postObj->MsgType) == 'event') {
            //如果是关注事件(subscribe)
            if (strtolower($postObj->Event == 'subscribe')) {
                //回复用户消息
                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time = time();
                $msgType = 'text';
                $content = '欢迎关注 宿迁市面包新语 微信公众号^_^';
                $template = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;
            }
        }
        //回复纯文本或单图文消息
        if (($postObj->MsgType) == 'text' && trim($postObj->Content) == '闺蜜') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;

            $arr = array(
                array(
                    'title' => '闺蜜挑战',
                    'description' => "参与真假塑料闺蜜活动,免费领取面包",
                    'picUrl' => 'http://xinyu.squmo.com/static/images/lulu.png',
                    'url' => 'http://xinyu.squmo.com/wx.php/Index/index',
                ),
            );
            $template = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <ArticleCount>" . count($arr) . "</ArticleCount>
                        <Articles>";
            foreach ($arr as $k => $v) {
                $template .= "<item>
                            <Title><![CDATA[" . $v['title'] . "]]></Title> 
                            <Description><![CDATA[" . $v['description'] . "]]></Description>
                            <PicUrl><![CDATA[" . $v['picUrl'] . "]]></PicUrl>
                            <Url><![CDATA[" . $v['url'] . "]]></Url>
                            </item>";
            }
            $template .= "</Articles>
                        </xml> ";
            echo sprintf($template, $toUser, $fromUser, time(), 'news');
        } else {
            $fromUser = $postObj->ToUserName;//消息从哪里来
            $toUser = $postObj->FromUserName;//发送给谁

            switch (trim($postObj->Content)) {
                case '兑换码':
                    $question_cdkey_model = M('question_cdkey');
                    $openid = $toUser->__toString(); // 关键将 simplexmlelement object to string
                    $question_info = $question_cdkey_model->where(['openid'=>$openid])->find();
                    if ($question_info) {
                        $content = $question_info['cdkey'];
                    } else {
                        $content = "抱歉,您还没有参加活动,回复【闺蜜】参与活动";
                    }

                    break;
                default:
                    $content = "欢迎品尝面包新语 回复【闺蜜】参与活动";
            }
            $template1 = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
            $time = time();
            $msgType = 'text';
            echo sprintf($template1, $toUser, $fromUser, $time, $msgType, $content);
        }
    }
}

很有意思!

他会根据事件,文本做出不同的处理!


猜你喜欢

转载自www.cnblogs.com/jiqing9006/p/9338394.html