[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() 28 { 29 //get post data, May be due to the different environments 30 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 31 32 //extract post data 33 //提取post数据 34 if (!empty($postStr)){ 35 36 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); 37 $fromUsername = $postObj->FromUserName;//发送人 38 $toUsername = $postObj->ToUserName;//接收人 39 $keyword = trim($postObj->Content);//消息内容 40 $time = time();//当前时间做为回复时间 41 42 $textTpl = "<xml> 43 <ToUserName><![CDATA[%s]]></ToUserName> 44 <FromUserName><![CDATA[%s]]></FromUserName> 45 <CreateTime>%s</CreateTime> 46 <MsgType><![CDATA[%s]]></MsgType> 47 <Content><![CDATA[%s]]></Content> 48 <FuncFlag>0</FuncFlag> 49 </xml>"; 50 if(!empty( $keyword )) 51 { 52 $msgType = "text"; 53 $contentStr = "lpc"; 54 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 55 echo $resultStr; 56 }else{ 57 echo "Input something..."; 58 } 59 60 }else { 61 echo ""; 62 exit; 63 } 64 } 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 return ‘NO‘; 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, $post_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 //加载常量文件 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 } 216 }

 


 

[php]微信测试号调取acces_token,自定义菜单以及被动响应消息

上一篇:让微信,qq,uc浏览器使用全屏模式,全屏模式里,浏览器是不会上下左右滑动出现背景的


下一篇:自制微信小程序 提示插件 -- noticeUitis.js