• PHP 操控微信公众号


    <?php
    class AutoAction extends CommonAction
    {
        public function index()
        {
            $timestamp = $_GET['timestamp'];//timestamp其实就是一个时间戳
            $nonce = $_GET['nonce'];//nonce是一个随机参数
            $token = "ATA2zJ3969DD99SlJKYJhy22j96GKKlB";//这个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->response_msg();
            }
        }
    
        private function response_msg()
        {
            $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
            $postObj = simplexml_load_string($postArr);
            if (strtolower($postObj->MsgType) == 'event') {
                //回复用户消息
                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                //如果是关注事件(subscribe)
                if (strtolower($postObj->Event == 'subscribe')) {
                    // 是否有推荐人
                    $referrer = $postObj->EventKey;
                    // 关注人openid
                    $openid = $toUser->__toString();
                    $referrer_str = $referrer->__toString();
                    if ($referrer_str) {
                        $referrer_openid = explode('_',$referrer_str)[1];
    
                        // 判断关注人是否已关注别人
                        $wx_recommend = M('wx_recommend');
                        $exist = $wx_recommend->where(['new_openid'=>$openid])->find();
                        if (!$exist) { // 有效的用户
                            // 添加记录
                            $data = [
                                'openid' => $referrer_openid,
                                'new_openid'  => $openid,
                                'add_time'=> time(),
                            ];
                            $wx_recommend->add($data);
                        }
    
                    }
    
                    $arr = array(
                        array(
                            'title' => '标题',
                            'description' => "描述",
                            'picUrl' => 'xxx.jpg',
                            'url' => 'xxx.html',
                        ),
                    );
                    $this->_send_news($arr,$toUser,$fromUser);
                } elseif ($postObj->Event == 'CLICK') {
                    //------------------- 点击事件 start ------------------------
                    $event_key = $postObj->EventKey; // 获取key
                    if($event_key=='V1001_PRESENT'){ 
                        $arr = array(
                            array(
                                'title' => '标题',
                                'description' => "描述",
                                'picUrl' => 'xxx.jpg',
                                'url' => 'xxx.html',
                            ),
                        );
                        $this->_send_news($arr,$toUser,$fromUser);
                    }
                    //------------------- 点击事件 end ------------------------
                }
    
            }
            //回复纯文本或单图文消息
            if (($postObj->MsgType) == 'text' && (trim($postObj->Content) == '我要帽子' || trim($postObj->Content) == '帽子')) {
                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
    
                $arr = array(
                    array(
                        'title' => '标题',
                        'description' => "描述",
                        'picUrl' => 'xxx.jpg',
                        'url' => 'xxx.html',
                    ),
                );
                $this->_send_news($arr,$toUser,$fromUser);
    
            } else if(($postObj->MsgType) == 'text' && (trim($postObj->Content) == '数量' || trim($postObj->Content) == '6')) {
                $fromUser = $postObj->ToUserName;//消息从哪里来
                $toUser = $postObj->FromUserName;//发送给谁
                // 用户openid
                $openid = $toUser->__toString();
                // 获取推荐数量
                $wx_recommend = M('wx_recommend');
                $count = $wx_recommend->where(['openid'=>$openid])->count();
                $content = "您当前已推荐关注人数为:".$count;
                $this->_send_text($content,$toUser,$fromUser);
            } else {
                    $fromUser = $postObj->ToUserName;//消息从哪里来
                    $toUser = $postObj->FromUserName;//发送给谁
                    $content = "内容";
                    $this->_send_text($content,$toUser,$fromUser);
            }
        }
    
        private function _send_text($content,$toUser,$fromUser) {
            $template = "<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($template, $toUser, $fromUser, $time, $msgType, $content);
        }
    
        private function _send_news($arr,$toUser,$fromUser) {
            $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> ";
    
            $time = time();
            $msgType = "news";
            echo sprintf($template, $toUser, $fromUser, $time, $msgType);
        }
    
    }
    
  • 相关阅读:
    8 pandas模块,多层索引
    7 numpy 傅里叶,提取图片轮廓
    6 DataFrame处理丢失数据--数据清洗
    5 pandas模块,DataFrame类
    4 pandas模块,Series类
    3 numpy模块
    2 线性代数基础
    1 Ipython、Jupyter 入门
    jdk安装与环境变量配置(一劳永逸)
    对jsp可见域的变量感悟
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10007043.html
Copyright © 2020-2023  润新知