一个微信接口类

 

<?php
class weiXin
{
    public $token;
    public $appID;
    public $appsecret;
//     public $data;
    
    public function __construct($appID, $appsecret)
    {
        $to2 = array();
        $this->appID = $appID;
        $this->appsecret = $appsecret;
        $this->token = $this->getToken($appID, $appsecret);
    }
    public function getToken($appID,$appsecret)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appID."&secret=".$this->appsecret;
        $to1 = $this->get_it($url);
        $to2 = (array)json_decode($to1);
        $this->token = $to2['access_token'];
        return $this->token;
    }
    /*验证签名*/
    public function valid(){
        if ($this->checkSignature()){
            return true;
        }else {
            return false;
        }
    }
    /*检查签名*/
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
    
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
    /*接收信息*/
    public function getMsg()
    {
        $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            return $postObj;
        }else {
            return false;
        }
        
    }

    /*响应消息处理*/
    public function responseMsg()
    {
        //接收传过来的信息
        $postObj = $this->getMsg();
        if (!empty($postObj)){
            //信息分离
            $MsgType = trim($postObj->MsgType);
            switch ($MsgType){
                case 'text':
                    $result = $this->recieveText($postObj);
                    break;
                case 'image':
                    $result = $this->recieveImage($postObj);
                    break;
                case 'voice':
                    $result = $this->recieveVoice($postObj);
                    break;
                case 'video':
                    $result = $this->recieveVideo($postObj);
                    break;
                case 'shortvideo':
                    $result = $this->recieveShortvideo($postObj);
                    break;
                case 'location':
                    $result = $this->recieveLocation($postObj);
                    break;
                case 'link':
                    $result = $this->recieveLink($postObj);
                    break;
                case 'event'://接收事件推送
                    $result = $this->recieveEvent($postObj);
                    break;
                default :
                    $result = 'undefined MsgType!'.$MsgType;
                    break;
            }
        }else {
            $result = '';
        }
        return $result;
    }
    //处理接收到的文本信息
    public function recieveText($postObj)
    {
       //接收传过来的关键词
       $keyword = trim($postObj->Content);
       //被动回复
       switch ($keyword){
           case '文本':
               $this->writeLog($postObj, 'recieveText.php');
               $content = '这是一个文本信息';
               $result = $this->replyText($postObj, $content);
               break;
           default :
               $result = '无相关内容';
               break;
       }
       return $result;
    }
    //处理接收到的图片信息
    public function recieveImage($postObj)
    {
        
    }
    //处理接收到的语音信息
    public function recieveVoice($postObj)
    {
        
    }
    //处理接收到的视频信息
    public function recieveVideo($postObj)
    {
        
    }
    //处理接收到的短视频信息
    public function recieveShortvideo($postObj)
    {
        
    }
    //处理接收到的位置信息
    public function recieveLocation($postObj)
    {
        
    }
    //处理接收到的链接信息
    public function recieveLink($postObj)
    {
        
    }
    //处理接收到的事件推送
    public function recieveEvent($postObj)
    {
        $event = $postObj->Event;
        switch ($event){
            case 'subscribe'://包括用户在未关注情况下的扫码事件
                $result = $this->handleSubscribe($postObj);
                break;
            case 'unsubscribe':
                $result = $this->handleSubscribe($postObj);
                break;
            case 'SCAN'://用户已关注情况下的扫码事件
                $result = $this->handleScan($postObj);
                break;
            case 'LOCATION':
                $result = $this->handleLocation($postObj);
                break;
            case 'CLICK':
                $result = $this->handleClick($postObj);
                break;
            case 'VIEW':
                $result = $this->handleView($postObj);
                break;
            default :
                $result = '相关服务暂不支持';
                break;
        }
        
    }
    //处理关注事件
    public function handleSubscribe($postObj)
    {
        //处理用户在未关注情况下的扫码事件
        if (isset($postObj->EventKey) && isset($postObj->Ticket)){
            
        }
    }
    //处理取消关注事件
    public function handleUnsubscribe($postObj)
    {
        
    }
    //处理用户在已关注情况下的扫码事件
    public function handleScan($postObj)
    {
        
    }
    //处理用户上传地理位置事件
    public function handleLocation($postObj)
    {
        
    }
    //处理用户点击菜单拉取消息时的事件推送
    public function handleClick($postObj)
    {
        
    }
    //处理用户点击菜单跳转链接时的事件推送
    public function handleView($postObj)
    {
        
    }
    //被动回复文本信息
    public function replyText($postObj,$content)
    {
        //模板
        $xmlTpl = '<xml>
                   <ToUserName><![CDATA[%s]]></ToUserName>
                   <FromUserName><![CDATA[%s]]></FromUserName>
                   <CreateTime>%s</CreateTime>
                   <MsgType><![CDATA[text]]></MsgType>
                   <Content><![CDATA[%s]]></Content>
                   </xml>';
        //组装回复的内容
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time(),$content);
        $this->writeLog($result, 'reply.xml');
        return $result;
    }
    //被动回复图片信息
    public function replyImage($postObj,$media_id)
    {
        $xmlTpl = ' <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[image]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Image>
                    </xml>';
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time(),$media_id);
        return $result;
    }
    //被动回复语音信息
    public function replyVoive($postObj,$media_id)
    {
        $xmlTpl = ' <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[voice]]></MsgType>
                    <Voice>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Voice>
                    </xml>';
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time(),$media_id);
        return $result;
        
    }
    //被动回复视频信息
    public function replyVedio($postObj,$content)
    {
        if (!isset($content['media_id'])){
            return '';
        }
        $extraTpl = '';
        if (isset($content['title'])){
            $tpl = '<Title><![CDATA[%s]]></Title>';
            $tpl = sprintf($tpl,$content['title']);
            $extraTpl = $tpl;
        }
        if (isset($content['description'])){
            $tpl = '<Description><![CDATA[%s]]></Description>';
            $tpl = sprintf($tpl,$content['description']);
            $extraTpl .= $tpl;
        }
        $xmlTpl = '<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[video]]></MsgType>
                <Video>
                <MediaId><![CDATA[%s]]></MediaId>
               '.$extraTpl.'
                </Video> 
                </xml>';
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time(),$content['media_id']);
        return $result;
    }
    //被动回复音乐信息
    public function replyMusic($postObj,$content)
    {
        $extraTpl = '';
        if (isset($content['title'])){
            $tpl = '<Title><![CDATA[%s]]></Title>';
            $tpl = sprintf($tpl,$content['title']);
            $extraTpl = $tpl;
        }
        if (isset($content['description'])){
            $tpl = '<Description><![CDATA[%s]]></Description>';
            $tpl = sprintf($tpl,$content['description']);
            $extraTpl .= $tpl;
        }
        if (isset($content['music_url'])){
            $tpl = '<MusicUrl><![CDATA[%s]]></MusicUrl>';
            $tpl = sprintf($tpl,$content['music_url']);
            $extraTpl .= $tpl;
        }
        if (isset($content['HQMusicUrl'])){
            $tpl = '<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>';
            $tpl = sprintf($tpl,$content['HQMusicUrl']);
            $extraTpl .= $tpl;
        }
        if (isset($content['media_id'])){
            $tpl = '<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>';
            $tpl = sprintf($tpl,$content['media_id']);
            $extraTpl .= $tpl;
        }
        if (!empty($extraTpl)){
            $extraTpl = '<Music>'.$extraTpl.'</Music>';
        }
        $xmlTpl = ' <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[music]]></MsgType>
                    '.$extraTpl.'
                    </xml>';
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time());
        return $result;
    }
    //被动回复图文信息
    public function replyNews($postObj,$items = array())
    {
        $articleCounts = count($items);
        if ($articleCounts >= 10){
            return '';
        }
        $itemsTpl = '';
        foreach ($items as $ik => $iv){
            $itemTpl = '';
            if (isset($iv['title'])){
                $tpl = '<Title><![CDATA[%s]]></Title>';
                $tpl = sprintf($tpl,$iv['title']);
                $itemTpl = $tpl;
            }
            if (isset($iv['description'])){
                $tpl = '<Description><![CDATA[%s]]></Description>';
                $tpl = sprintf($tpl,$iv['description']);
                $itemTpl .= $tpl;
            }
            if (isset($iv['picUrl'])){
                $tpl = '<PicUrl><![CDATA[%s]]></PicUrl>';
                $tpl = sprintf($tpl,$iv['picUrl']);
                $itemTpl .= $tpl;
            }
            if (isset($iv['url'])){
                $tpl = '<Url><![CDATA[%s]]></Url>';
                $tpl = sprintf($tpl,$iv['url']);
                $itemTpl .= $tpl;
            }
            if (!empty($itemTpl)){
                $itemTpl = '<item>'.$itemTpl.'</item>';
                $itemsTpl .= $itemTpl;
            }
        }
        $xmlTpl = ' <xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>%s</ArticleCount>
                    <Articles>
                    '.$itemsTpl.'
                    </Articles>
                    </xml> ';
        $result = sprintf($xmlTpl,$postObj->FromUserName,$postObj->ToUserName,time(),$articleCounts);
        return $result;
        
    }
    public function createMenu($data)
    {
       $url =  "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->token;
       $result = $this->post_it($url,$data);
       return $result;
    }
    public function queryMenu()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->token;
        $result = $this->get_it($url);
        return $result;
    }
    public function delMenu()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->token;
        $result = $this->get_it($url);
    }
    public function post_it($url, $data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
        
    }
    public function get_it($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    //记录日志
    private function writeLog($log_content,$filename)
    {
    
        $max_size = 100000;   //声明日志的最大尺寸
    
        $log_filename = dirname(__FILE__).'/log/'.$filename;  //文件名
    
        //如果文件存在并且大于了规定的最大尺寸就删除
        if(file_exists($log_filename) && (abs(filesize($log_filename)) > $max_size)){
            unlink($log_filename);
        }
        //写入日志,内容前加上时间, 后面加上换行, 以追加的方式写入
        file_put_contents($log_filename, date('H:i:s')." ".$log_content."\n", FILE_APPEND);
    
    }
}

 

转载于:https://my.oschina.net/u/3238067/blog/829302

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值