微信公众号被关注回复、自动回复

32F  2019/01/03 php



没有啰嗦的必要,code down

<?php
/**
 * wechat php test
 */
//define your token
define("TOKEN", "*****");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest {
    private $token;
    private $appId;
    private $appSecret;
    public function valid() {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }
    public function responseMsg() {

        $this->append(date('YmdHis'));
        //get post data, May be due to the different environments
//        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //PHP7已经移除这个全局变量 用下面的代替
        $postStr = file_get_contents('php://input');

        //extract post data
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $msgType = $postObj->MsgType;//消息类型
            $event = $postObj->Event;
            $time = time();
            $this->append([$fromUsername,$toUsername]);
            $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>";

            $otherRsp =  [
                "不要吵,I'm playing War3,对手上岛中...(网易对战平台用户名:32F,来战吧~)",
                '快过年了~可以休息2个星期~',
                '约战竞技场,97高清对战,还算有意思',
                '正在探索无尽的黑魂3~因为难所以无尽',
                '正在复习系统分析师~明年软考加油',
                '我是一名普通的php开发工程师,有什么可以为您效劳的吗,2260891938@qq.com来邮哦~',
                '2019年有没有值得期待的ps4作品,例如之狼、鬼泣5。',
                '想不想看看youtube?玩玩推特?求我啊~',
                '幸运奖,回复4再转吧'
            ];

            switch($msgType){
                case "event":
                    //关注回复
                    if($event=="subscribe"){
                        $data = file_get_contents("https://www.wdsst.com/addons/ws_multi_shop/core/task/response/response.php");
                        $data = json_decode($data,true);
                        $data = $data['result'];
                        $this->getAccessToken();
                        $nickname= $this->getMemberInfo($fromUsername);
                        $contentStr = "Hi~".$nickname." 谢谢你的关注,如果有什么需要,留下点什么吧~\n\n";
                        $link = "";
                        $link .= "<a href='https://www.xxroom.xyz'>本站首页</a>\n";
                        $link .= "主站网址:回复 '1'\n";
                        $link .= "本站功能:回复 '2'\n";
                        $link .= "问问魔镜谁是世界上最有趣的人:回复 '3'\n";
                        $link .= "幸运大转盘:回复 '4'\n";

                        $contentStr .= $link;
                    }
                    break;
                case "text":
                    switch($keyword){
                        case "1":
                            $contentStr = "官网地址:"."\n"."https://www.xxroom.xyz";
                            break;
                        case "2":
                            $contentStr = "本站功能:"."\n"."导航常用链接、博客分享、快速问答、学习交流...";
                            break;
                        case "3":
                            $contentStr = "假装思索,有一点可以确认,问这个问题的人好傻~\n";
                            break;
                        default:
                            $n = rand(0,8);
                            $contentStr = $otherRsp[$n];
                    }
                    break;
            }
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        } else {
            echo "";
            exit;
        }
    }


    private function getAccessToken(){
        $this->appId = "******";
        $this->appSecret = "*******";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
        $rst = file_get_contents($url);
        $rst = json_decode($rst,true);
        $this->token =  $rst['access_token'];
        return;
    }

    private function getMemberInfo($openid){
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->token."&openid=".$openid."&lang=zh_CN";
        $this->append($url);
        $rst = file_get_contents($url);
        $this->append($rst);
        $rst = json_decode($rst,true);
        $nickname =  $rst['nickname'];
        return $nickname;
    }

    //日志
    private function append($data){
        if(is_array($data)){
            $data = implode(',',$data);
        }
        file_put_contents('aaa.log',$data."\n",FILE_APPEND);
    }

    //微信认证服务器时用的
    private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}
?>

添加评论
全部回复
暂无回复