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

1 <?php2 /**自己写的

*/

3 $wechatObj = newwechatCallbackapiTest();4 $wechatObj->valid();5 $wechatObj->responseMsg();//响应消息6 $wechatObj->set_menu();//自定义菜单7

8 classwechatCallbackapiTest9 {10 /**

11 * 绑定url、token信息

12 */

13 public functionvalid()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 functionresponseMsg()28 {29 //get post data, May be due to the different environments

30 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];31

32 //extract post data33 //提取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 = "43 44 45 %s46 47 48 049 ";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 functioncheckSignature()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 functionset_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 functioncheck_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 functionget_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 functionread_access_token()201 {202 $token = unserialize(file_get_contents(‘token.txt‘));203 return $token[‘access_token‘];204 }205

206 //存token方法

207 public functionmem_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 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值