关于微信平台开发者学习

<?php

class wechatCallbackapiTest
{
    public $access_token;  //作为一个数组存储,‘AccToken‘存储获得的access token,‘deadtime‘存储有效时间,‘CTime‘记录获得的access token的时间
    private $menu;   //存放自定义菜单,数组类型
    
    function __construct()
    {
      //$this->get_AccToken();
      //$this->delete_menu();
      //$this->create_menu();
      $this->responseMsg();
    }
    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
                
                  $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                
                $type=trim($postObj->MsgType);
                switch($type)
                {
                  case "text":
                        $ret=$this->handle_text($postObj); 
                        break;
                  case "image":
                        $ret=$this->handle_pic($postObj);
                        break;
                  case "event":
                        $ret=$this->handle_event($postObj);
                        break;
                  case "voice":
                        $ret=$this->handle_voice($postObj);
                        break;
                  case "link":
                        $ret=$this->handle_link($postObj);
                        break;
                  case "location":
                        $ret=$this->handle_location($postObj);
                        break;
                  default:
                        $contentStr = "未识别的消息";
                        $ret=$this->res_text($postObj,$content);
                 }
                 echo $ret;
        }
        else 
        {
            echo "";
            exit;
        }
    }
  
    private function res_text($postObj,$content)  //回复一条文字消息
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    </xml>";
        $msgType = "text";
        $contentStr=$content;
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        return $resultStr;        
    }

    private function handle_text($postObj)  //处理接收到的文字消息  test
    {
        $keyword = trim($postObj->Content); 
        if(!empty( $keyword ))
        {
            $content = "这是一条文字消息";
            $ret=$this->res_text($postObj,$content);
            return $ret;
        }
        else
        {
            echo "Input something...";
        }
    }
  
   /*private function handle_pic($postObj)
    {
        $content= "这是一条图片消息";
        $ret=$this->res_text($postObj,$content);
        return $ret;        
    }*/
    private function handle_pic($postObj)
    
    {
        $MediaID = $postObj->MediaId;
        /*$fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[media_id]]></MediaId>
                    </Image>
                    </xml>";
        $msgType = "image";    
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $image);
        return $resultStr;*/
        $ret=$this->res_text($postObj,$MediaID);
        return $ret;
    }
    
    private function handle_event($postObj)
    {
        $event=$postObj->Event;
        switch($event)
        {
           case "CLICK":
                $eventkey=trim($postObj->EventKey);
                switch($eventkey)
                {
                      case "yingxin":
                          $content= "你点击的是迎新特刊";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "jieri":
                          $content= "你点击的是节日特刊";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "zhoumo":
                          $content= "你点击的是周末特刊";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "gesai":
                          $content= "校园歌手大赛";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "gehui":
                          $content= "毕业歌会";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "yuedu":
                          $content= "光华悦读读书季";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "jinisi":
                          $contentStr = "工大吉尼斯";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "tingxie":
                          $content= "听写大赛";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "weiquanfangshi":
                          $content= "     ———维权方式———\n维权邮箱:\n    weiquan@bjut.edu.cn\n维权电话:\n       13161106308\n\n ***全心权益,为你服务***";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "weiquanchengguo":
                          $content= "你点击的是维权成果";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                      case "woyaoweiquan":
                          $content= "     ———我要维权———\n如果你在校园生活当中遇到权益问题。\n回复发送:“维权”+内容。\n我们将努力为你解决。\n\n ***全心权益,为你服务***";
                          $ret=$this->res_text($postObj,$content);
                          return $ret;
                }
        }
    }
    
    /*private function handle_voice($postObj)
    {
        if (!empty($postObj->Recognition))
        $content=$postObj->Recognition;
        $ret=$this->res_text($postObj,$content);
        return $ret;
        
    }*/
    
    private function handle_voice($postObj)
    {   
        $MediaID = $postObj->MediaId;
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[voice]]></MsgType>
                    <Voice>
                    <MediaId><![CDATA[media_id]]></MediaId>
                    </Voice>
                    </xml>";
        $msgType = "voice";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $MediaID);
        return $resultStr;    
    }
    
    private function handle_link($postObj)
    {
        $contentStr ="这是一条链接消息" ;
        $ret=$this->res_text($postObj,$content);
        return $ret;
        
    }
     
    private function handle_location($postObj)
    {
        $contentStr ="这是一个位置消息" ;
        $ret=$this->res_text($postObj,$content);
        return $ret;
    }
   
    public function get_AccToken()   //用于获取access token
    {
        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
        $json=file_get_contents($TOKEN_URL);
        $result=json_decode($json,true);
        $this->access_token=array(‘AccToken‘=>$result[‘access_token‘],‘deadtime‘=>$result[‘expires_in‘],‘CTime‘=>time(),);
        return $this->access_token;
    }
        
    private function legal_time()  //判断access token的时间是否有效   
    {
        $cur=time();  //获取当前时间
        if($cur-$this->access_token[‘CTime‘]>=$this->access_token[‘deadtime‘])  //当前时间减去获取时间大于等于有效期时失效,自动重新请求
        {
           $this->get_AccToken();
        }
    }
    
    private function create_menu()
    {
      $this->menu=‘ {
    "button": [
        {
            "name": "毕业歌会", 
            "sub_button": [
                {
                    "type": "click", 
                    "name": "红色", 
                    "key": "yingxin"
                }, 
                {
                    "type": "click", 
                    "name": "橙色", 
                    "key": "jieri"
                }, 
                {
                    "type": "click", 
                    "name": "紫色", 
                    "key": "zhoumo"
                },
                {
                    "type": "click", 
                    "name": "蓝色", 
                    "key": "yingxin"
                },
                {
                    "type": "click", 
                    "name": "绿色", 
                    "key": "yingxin"
                }
            ]
        }, 
        {
            "name": "精彩活动", 
            "sub_button": [
                {
                    "type": "click", 
                    "name": "校园歌手大赛", 
                    "key": "gesai"
                }, 
                {
                    "type": "click", 
                    "name": "毕业歌会", 
                    "key": "gehui"
                }, 
                {
                    "type": "click", 
                    "name": "光华悦读读书季", 
                    "key": "yuedu"
                }, 
                {
                    "type": "click", 
                    "name": "工大吉尼斯", 
                    "key": "jinisi"
                }, 
                {
                    "type": "click", 
                    "name": "听写大赛", 
                    "key": "tingxie"
                }
            ]
        }, 
        {
            "name": "全心权益", 
            "sub_button": [
                {
                    "type": "click", 
                    "name": "维权方式", 
                    "key": "weiquanfangshi"
                }, 
                {
                    "type": "click", 
                    "name": "维权成果", 
                    "key": "weiquanchengguo"
                }, 
                {
                    "type": "click", 
                    "name": "我要维权", 
                    "key": "woyaoweiquan"
                }
            ]
        }
    ]
}
;

       $m=urldecode(json_encode($this->menu));
       $this->legal_time();
       $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token[‘AccToken‘];
       //$rt=ltrim($MENU_URL,"https://api.weixin.qq.com/cgi-bin/menu/create");
       $ch = curl_init(); 

       curl_setopt($ch, CURLOPT_URL, $MENU_URL); 
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)‘);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
       curl_setopt($ch, CURLOPT_POSTFIELDS,$this->menu);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

       $info = curl_exec($ch);
       $retData=json_decode($info,true);
       $ret=$retData[‘errcode‘];
       return $ret;
    }
    
    private function query_menu()
    {
        $QUR_URL="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN".$this->access_token[‘AccToken‘];
        $json=file_get_contents($QUR_URL);
        $result=json_decode($json,true);
        return $result;
    }
    
    private function delete_menu()
    {
        $DEL_URL="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->access_token[‘AccToken‘];
        $json=file_get_contents($DEL_URL);
        $result=json_decode($json,true);
        $ret=$result[‘errcode‘];
        return $ret;
    }
}

首先,在这个项目中我采用了百度云服务器进行编辑和发布

了解了关于TOKEN的发送,还有微信端和服务器之间的联系

关于微信平台开发者学习

上一篇:html中的点击事件


下一篇:【Bugly干货分享】微信文件微起底Ⅰ