zoukankan      html  css  js  c++  java
  • [php]微信测试号调取acces_token,自定义菜单以及被动响应消息

      1 <?php
      2 /**自己写的
      */ 3 $wechatObj = new wechatCallbackapiTest(); 4 $wechatObj->valid(); 5 $wechatObj->responseMsg();//响应消息 6 $wechatObj->set_menu();//自定义菜单 7 8 class wechatCallbackapiTest 9 { 10 /** 11 * 绑定url、token信息 12 */ 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 /** 25 * 接收消息,并自动发送响应信息 26 */ 27 public function responseMsg()
        {
            //get post data, May be due to the different environments
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

              //extract post data
              //提取post数据  
            if (!empty($postStr)){
                    
                      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                    $fromUsername = $postObj->FromUserName;//发送人
                    $toUsername = $postObj->ToUserName;//接收人
                    $keyword = trim($postObj->Content);//消息内容
                    $time = time();//当前时间做为回复时间

                    $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>";             
                    if(!empty( $keyword ))
                    {
                          $msgType = "text";
                          // 天气预报
                          if (strlen($keyword)==6) {
                              if ($keyword=='天气') {
                                  $contentStr = "PengchongLee的商城欢迎你! 温馨小提示: 输入【天气+城市】可获取实时天气! 例如:天气北京";
                              }else{
                                  $contentStr = "PengchongLee的商城欢迎你! 温馨小提示: 输入【天气+城市】可获取实时天气! 例如:天气北京";
                              }                    
                        }elseif (strlen($keyword)>=12) {
                            $city = substr($keyword,6);
                            $weather = $this->getWeather($city);
                            if ($weather['success']==0) {
                                $contentStr = "PengchongLee的商城欢迎你! 温馨小提示: 输入【天气+城市】可获取实时天气! 例如:天气北京";
                            }elseif($weather['success']==1){
                                $contentStr = "实时天气: 城市:【".$weather['result']['citynm']."】; 日期:【".
                                $weather['result']['days']."】; 星期:【".$weather['result']['week']."】; 温度:【".
                                $weather['result']['temperature']."】; 天气:【".$weather['result']['weather']."】;";
                            }
                        }else{
                            $contentStr = "PengchongLee的商城欢迎你! 温馨小提示: 输入【天气+城市】可获取实时天气! 例如:天气北京";
                        }
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                    }else{
                        echo "Input something...";
                    }

            }else {
                echo "";
                exit;
            }
        }
    65 66 /** 67 * 检查签名,确保请求是从微信发过来的 68 */ 69 private function checkSignature() 70 { 71 $signature = $_GET["signature"]; 72 $timestamp = $_GET["timestamp"]; 73 $nonce = $_GET["nonce"]; 74 75 $token = TOKEN; 76 $tmpArr = array($token, $timestamp, $nonce); 77 sort($tmpArr); 78 $tmpStr = implode( $tmpArr ); 79 $tmpStr = sha1( $tmpStr ); 80 81 if( $tmpStr == $signature ){ 82 return true; 83 }else{ 84 return false; 85 } 86 } 87 88 /** 89 * 自定义菜单 90 */ 91 public function set_menu() 92 { 93 $access_token = $this->check_token(); 94 95 if ($access_token=='no') { 96 97 }else{ 98 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token"; 99 100 $post_data = ' 101 { 102 "button":[ 103 { 104 "type":"view", 105 "name":"首页", 106 "url":"http://www.lpcblog.com/weixin/shop/" 107 }, 108 { 109 "name":"个人中心", 110 "sub_button":[ 111 { 112 "type":"view", 113 "name":"个人信息", 114 "url":"http://www.lpcblog.com/weixin/shop/user.html" 115 }, 116 { 117 "type":"view", 118 "name":"个人账户", 119 "url":"http://www.lpcblog.com/weixin/shop/myuser.html" 120 }] 121 }, 122 { 123 "type":"click", 124 "name":"关于我们", 125 "key":"V1001_TODAY_MUSIC" 126 } 127 ] 128 }'; 129 130 //设置菜单也是post传值 131 return json_decode($this->curl($url,$post_data); 132 } 133 } 134 135 //判断token值时效方法 136 public function check_token() 137 { 138 if (file_exists('token.txt')) 139 { 140 //判断token值时效,修改时间 141 $mtime=filemtime("token.txt"); 142 if((time()-$mtime)<7000){ 143 return $this->read_access_token(); 144 }else{ 145 $this->mem_token();
    return $this->read_access_token(); 146 } 147 }else{ 148 $this->mem_token(); 149 return $this->read_access_token(); 150 } 151 } 152 153 //curl封装类 154 public function curl($url,$data=array()) 155 { 156 // 初始化curl 157 $ch = curl_init(); 158 159 curl_setopt($ch, CURLOPT_URL, $url); 160 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 161 162 // 开启支持https 163 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 164 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 165 // post数据 166 curl_setopt($ch, CURLOPT_POST, 1); 167 // post的变量 168 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 169 170 $output = curl_exec($ch); 171 // 关闭curl 172 curl_close($ch); 173 return $output; 174 } 175 176 //获取access_token方法 177 public function get_token() 178 { 179 //加载常量文件(这里是定义的申请的appid等常量) 180 include('define.php'); 181 //微信获取access_token地址 182 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APP_SECRET; 183 184 //传值方式POST 185 $post_data = array( 186 'grant_type'=>'client_credential', 187 'appid'=>APPID, 188 'client_secret'=>APP_SECRET, 189 ); 190 //curl方法模拟提交获取access_token(格式json) 191 $access_token = json_decode($this->curl($url,$post_data); 192 if($access_token['access_token']){ 193 return $access_token; 194 }else{ 195 return "获取access_token失败"; 196 } 197 } 198 199 //读取access_token的方法 200 public function read_access_token() 201 { 202 $token = unserialize(file_get_contents('token.txt')); 203 return $token['access_token']; 204 } 205 206 //存token方法 207 public function mem_token() 208 { 209 //调用获取access_token的方法 210 $access_token = $this->get_token(); 211 //序列化返回的access_token 212 $txt = serialize($access_token); 213 //保存access_token 214 file_put_contents('token.txt',$txt); 215 }
        /**
     * 获取天气
     */
        public function getWeather($city='')
        {
            //加载常量文件
            include('define.php');
            //实时天气接口
            $url = "http://api.k780.com/?app=weather.today&weaid=".$city."&appkey=".NOWAPI_APPKEY."&sign=".NOWAPI_SIGN."&format=json";
            $post_data = '';
            $weather = json_decode($this->curl($url),true);
            return $weather;
        }
    216 }

  • 相关阅读:
    【书目20200917】影响力思维
    Spring AOP +自定义注解 + Spel表达式 实现审计日志
    SPRING BOOT 注解之OBJECTPROVIDER源码追踪
    Activiti~相关概念
    ELK~fluentd多行日志的收集
    k8s~向etc/hosts里添加内容
    maven~本地仓库的指定
    ssh~ Access denied问题解决
    MySQL如何绕过授予information_schema中对象时报ERROR 1044(4200)错误
    Zabbix如何监控SQL Server服务状态
  • 原文地址:https://www.cnblogs.com/lipcblog/p/6978982.html
Copyright © 2011-2022 走看看