[php]微信测试号调取acces_token,自定义菜单以及被动响应消息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[php]微信测试号调取acces_token,自定义菜单以及被动响应消息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4728字,纯文字阅读大概需要7分钟。
内容图文
![[php]微信测试号调取acces_token,自定义菜单以及被动响应消息](/upload/InfoBanner/zyjiaocheng/1050/fb523d91da6d42d49d866f0cf21f82ec.jpg)
1 <?php 2 /**自己写的
*/ 3$wechatObj = new wechatCallbackapiTest(); 4$wechatObj->valid(); 5$wechatObj->responseMsg();//响应消息 6$wechatObj->set_menu();//自定义菜单 7 8class wechatCallbackapiTest 9{ 10 /** 11 * 绑定url、token信息 12 */ 13publicfunction valid() 14 { 15$echoStr = $_GET["echostr"]; 16 17//valid signature , option 18if($this->checkSignature()){ 19echo$echoStr; 20exit; 21 } 22 } 23 24 /** 25 * 接收消息,并自动发送响应信息 26 */ 27publicfunction 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数据 34if (!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>"; 50if(!empty( $keyword )) 51 { 52$msgType = "text"; 53$contentStr = "lpc"; 54$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 55echo$resultStr; 56 }else{ 57echo "Input something..."; 58 } 59 60 }else { 61echo ""; 62exit; 63 } 64 } 65 66 /** 67 * 检查签名,确保请求是从微信发过来的 68 */ 69privatefunction checkSignature() 70 { 71$signature = $_GET["signature"]; 72$timestamp = $_GET["timestamp"]; 73$nonce = $_GET["nonce"]; 74 75$token = TOKEN; 76$tmpArr = array($token, $timestamp, $nonce); 77sort($tmpArr); 78$tmpStr = implode( $tmpArr ); 79$tmpStr = sha1( $tmpStr ); 80 81if( $tmpStr == $signature ){ 82returntrue; 83 }else{ 84returnfalse; 85 } 86 } 87 88 /** 89 * 自定义菜单 90 */ 91publicfunction set_menu() 92 { 93$access_token = $this->check_token(); 94 95if ($access_token==‘no‘) { 96 97 }else{ 98$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token"; 99100$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 }‘; 129130//设置菜单也是post传值131return json_decode($this->curl($url,$post_data); 132 } 133 } 134135//判断token值时效方法136publicfunction check_token() 137 { 138if (file_exists(‘token.txt‘)) 139 { 140//判断token值时效,修改时间141$mtime=filemtime("token.txt"); 142if((time()-$mtime)<7000){ 143return$this->read_access_token(); 144 }else{ 145return ‘NO‘; 146 } 147 }else{ 148$this->mem_token(); 149return$this->read_access_token(); 150 } 151 } 152153//curl封装类154publicfunction curl($url,$data=array()) 155 { 156// 初始化curl157$ch = curl_init(); 158159 curl_setopt($ch, CURLOPT_URL, $url); 160 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 161162// 开启支持https163 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); 169170$output = curl_exec($ch); 171// 关闭curl172 curl_close($ch); 173return$output; 174 } 175176//获取access_token方法177publicfunction get_token() 178 { 179//加载常量文件180include(‘define.php‘); 181//微信获取access_token地址182$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APP_SECRET; 183184//传值方式POST185$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); 192if($access_token[‘access_token‘]){ 193return$access_token; 194 }else{ 195return "获取access_token失败"; 196 } 197 } 198199//读取access_token的方法200publicfunction read_access_token() 201 { 202$token = unserialize(file_get_contents(‘token.txt‘)); 203return$token[‘access_token‘]; 204 } 205206//存token方法207publicfunction mem_token() 208 { 209//调用获取access_token的方法210$access_token = $this->get_token(); 211//序列化返回的access_token212$txt = serialize($access_token); 213//保存access_token214file_put_contents(‘token.txt‘,$txt); 215 } 216 }
原文:http://www.cnblogs.com/lipcblog/p/6978982.html
内容总结
以上是互联网集市为您收集整理的[php]微信测试号调取acces_token,自定义菜单以及被动响应消息全部内容,希望文章能够帮你解决[php]微信测试号调取acces_token,自定义菜单以及被动响应消息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。