欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信公共账号开发模式PHP接口实现

程序员文章站 2022-04-09 12:26:10
...
<?php
define("TOKEN","你的TOKEN");
class wechatCallBackApi{
    //public $signature;    //加密签名
    //public $timestamp;    //时间戳
    //public $nonce;        //随机数
    //public $echostr;    //随机字符串,返回标志
    public $fromUserName;    //发信人ID
    public $toUserName;        //收信人ID(本公共号)
    public $keyWords;        //文本信息
    public $msgId;        //消息ID
    public $welcomeMsg; //欢迎消息
    public $msgType;    //消息类型
    public $event;        //事件名
    public $mediaId;    //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
    public $format;        //语音格式:amr
    public $recognition;    //语音识别结果
    public function __construct(){
        $this->welcomeMsg="HELLO,欢迎订阅!";
            //$this->main();    //*****************第一次验证的时候打开,随后可关闭***************
    }
    //首次TOKEN验证
    public function main(){
        $echoStr=$_GET['echostr'];
        if($this->checkRes()){
            echo $echoStr;
        }else{
            exit();
        }
    }
    
    //验证权限
    private function checkRes(){
        $signature=$_GET['signature'];    //获取微信服务发送过来的参数
        $timestamp=$_GET['timestamp'];
        $nonce=$_GET['nonce'];
        $token = TOKEN;
        $tmpArr = array($token,$timestamp,$nonce);
        sort($tmpArr);    //进行字典排序
        $strWaitCheck = implode($tmpArr);    //进行字符串组合
        $strWaitCheck = sha1($strWaitCheck);    //对字符串进行sha1加密
        if($strWaitCheck==$signature){
            return true;
        }else{
            return false;
        }
    }
    public function getWhatWeChatServerPost(){
            $huntPostContent = $GLOBALS["HTTP_RAW_POST_DATA"];    //获取微信服务post过来的内容
            $newXmlObj =simplexml_load_string($huntPostContent,'SimpleXMLElement', LIBXML_NOCDATA);    //把post过的字符串载入对象
            $this->fromUserName = $newXmlObj->FromUserName;    //向公共账号发信息的用户名
            $this->toUserName = $newXmlObj->ToUserName;    //公共账号名
            $this->keyWords = trim($newXmlObj->Content);    //消息内容
            $this->msgId = trim($newXmlObj->MsgId);        //读取当前消息msgId
            $this->msgType = trim($newXmlObj->MsgType);        //读取当前消息类型
            $this->event = trim($newXmlObj->Event);        //读取当前事件名
            
            $this->format = trim($newXmlObj->Format);    //语音格式:amr
            
            $this->mediaId = trim($newXmlObj->MediaID);    //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
            
            $this->recognition = trim($newXmlObj->Recognition);    //语音识别结果,UTF8编码
        }
    //消息响应
    public function responseMsg(){
            $this->getWhatWeChatServerPost();    //执行接收微信服务器POST过来的东西
            
                    //当消息类型为text的时候处理
                    if(!empty($this->keyWords)&& $this->msgType == "text"){
                            $para = $this->keyWords;
                            switch($para){
                                    case "1":
                                    $tmp_item = $this->textPicMaxTmp(
                                    "this is a test!!",
                                    "各位大叔大妈别太大较真,这就一测试滴",
                                    "http://www.baidu.com/img/bdlogo.gif",
                                    "http://www.baidu.com/");
                                    $re_global = $this->resTextPicMax($this->fromUserName,$this->toUserName,1,$tmp_item);
                                    echo $re_global;
                                    break;
                                case "2":
                                    $contentResText = "Hello,buddy,you send me a \"2\"[玫瑰]";
                                    $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
                                    echo $re_global;
                                    break;
                                    
                                case "000":    
                                    $contentResText = "你想返回的字符串";
                                    $this->longStrHandleExtendfunc($contentResText);
                                    break;                  
                                default:
                                    //调用功能判断函数处理
                                    $this->longStrHandle($para);
                                       break;
                                }
                        }else if($this->msgType == "event" && !empty($this->event)){        //获取事件类型
                                    if($this->event=='subscribe'){
                                            $contentResText = $this->welcomeMsg;    //初次关注的时候推送欢迎消息
                                            $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
                                        }else if($this->event == "unsubscribe"){///这个地方好像没有作用!!!
                                            $contentResText = "我擦,你咋跑了";
                                            $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
                                            }
                            }else if($this->msgType == "voice"){
                                        if($this->recognition == ""){
                                                $contentResText ="亲,你说的火星文么,听不懂,建议您还是文字查询吧..[呲牙]";//语音无返回结果的时候给用记返回一条错误
                                                $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
                                            }else{
                                                $contentResText =$this->recognition; //其它情况下返回欢迎语
                                                $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
                                                    
                                                }
                                    
                                }else{
                            //如果在text情况意外得到一条空消息的处理
                            echo "";
                            exit();
                            }
                    echo $re_global;    //返回值给微信服务器
        }
            
            //多次用到的几句话就定义了一个小方法
            public function longStrHandleExtendfunc($conText){
                    $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$conText);
                    echo $re_global;
                }
                
            //收到除特定字符外的其它字符的处理方法,这个要根据你需要的具体逻辑处理    
            public function longStrHandle($waitHandleStr){
                    /*
                        your code
                    */
                            }
        //回复文本信息方法
        public function resTextMsg($tu,$fu,$textContent){
                //文本消息模板
                    $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>";
                    $msgType = "text";    //消息类型
                    $time = time();        //时间戳
                    $resServerStr = sprintf($textTpl,$tu,$fu,$time,$msgType,$textContent);    //替换模板里的内容
                    return $resServerStr;
            }
        //回复图文
        public function resTextPicMax($tu,$fu,$itemNum,$totalItemStr){
                //图片模板
                    $picTpl = "<xml>
                                     <ToUserName><![CDATA[%s]]></ToUserName>
                                     <FromUserName><![CDATA[%s]]></FromUserName>
                                     <CreateTime>%s</CreateTime>
                                     <MsgType><![CDATA[%s]]></MsgType>
                                     <ArticleCount>%s</ArticleCount>
                                     <Articles>"
                                    .$totalItemStr.
                                     "</Articles>
                                 </xml>";
                    $msgType = "news";    //消息类型
                    $time = time();        //时间戳
                    $resServerStr = sprintf($picTpl,$this->fromUserName,$this->toUserName,$time,$msgType,$itemNum);    //替换模板里的内容
                    return $resServerStr;
            }
            //图文模板处理
            public function textPicMaxTmp($title,$des,$picUrl,$jumpUrl){
                    //图文消息条目模板
                $itemTpl="
                         <item>
                                     <Title><![CDATA[%s]]></Title>
                                     <Description><![CDATA[%s]]></Description>
                                     <PicUrl><![CDATA[%s]]></PicUrl>
                                     <Url><![CDATA[%s]]></Url>
                                     </item>
                ";
                $resItemStr = sprintf($itemTpl,$title,$des,$picUrl,$jumpUrl);    //替换模板里的内容
                return $resItemStr;
                }
            //接下来就是你自己定义的各种方法
            
      
}