Nov
06
2016
微信开发之基础篇(二)
上一篇我们讲了自定义菜单,那么现在我们就来讲一讲微信端最重要的一个功能----微信授权登录
由于截图内容看上去不多,我就用文字介绍.
第一步,准备一个服务器和一个备案域名(这个很重要)
第二步,打开接口权限,在基本配置下方,找到"网页授权",进入之后会跳到一个页面,找到网页授权域名,设置成准备好的域名地址
第三步,验证token--这里的token在基本配置里面
先自定义一个token,这个无特殊要求
然后在自己的php文件里写入下面的方法
public function bbb(){ define('TOKEN','ninhHMq7tnKFkK6rWHhJIuMcKXNeEMZk');//这个你在微信里配置写的是什么 token 这个就 定义成什么 就可以直接用了 if (!isset($_GET['echostr'])) { $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "" ; //获取用户操作微信返回的数据 if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $MsgType = trim($postObj->MsgType); //if (($postObj->MsgType == "event") && ($postObj->Event == "unsubscribe")) continue; switch ($MsgType){ case "event"://这里是回复事件的 $result = self::receiveEvent($postObj); break; case "text"://这个是回复文本的 $result = self::receiveText($postObj); break; default: $result = "unknown msg type: ".$MsgType; break; } ob_clean(); echo $result; } }else{//这个地方是验证token的 也就是微信的第一次验证 /*微信get验证*/ $signature = $_GET["signature"]; //微信加密签名 $timestamp = $_GET["timestamp"]; //时间戳 $nonce = $_GET["nonce"]; //随即数 $echoStr = $_GET["echostr"]; //随机字符串 $token = TOKEN; //自定义验证字符 $tmpArray = array($token, $timestamp, $nonce); sort($tmpArray,SORT_STRING); $tmpString = implode($tmpArray); //排序后数组还原字符串 $tmpString = sha1($tmpString); //加密字符串 if($tmpString == $signature){ ob_clean(); echo $echoStr; exit; } } }
上传到服务器,用域名请求这个方法,同时在基本配置里面修改URL 指向这个方法的地址,点击确认.如果出现不成功多试几次,微信出错的可能性也很大.
验证成功之后,我们就可以用微信的授权登录方法了,主要有base和user_info,前者是只获取openId,无请求,后者获取用户的基本信息,需用户同意.
然后我们就用自定义菜单的url做成第一步,请求获取授权的code(这个地方多练练,别上手就直接拿方法)
下面是获取微信授权的集成方法
$code = $_GET['code']; $state = $_GET['state']; //换成自己的接口信息 $appid = ; $appsecret =; if (empty($code)) $this->error('授权失败'); $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $token = json_decode(file_get_contents($token_url)); // p($token_url); if (isset($token->errcode)) { echo '<h1>错误:</h1>'.$token->errcode; echo '<br/><h2>错误信息:</h2>'.$token->errmsg; exit; } $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; //转成对象 $access_token = json_decode(file_get_contents($access_token_url)); // p($access_token_url); // p($access_token); if (isset($access_token->errcode)) { echo '<h1>错误:</h1>'.$access_token->errcode; echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg; exit; } $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'; //转成对象 $user_info = json_decode(file_get_contents($user_info_url)); if (isset($user_info->errcode)) { echo '<h1>错误:</h1>'.$user_info->errcode; echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg; exit; } // p($user_info); //打印用户信息 // echo '<pre>'; // print_r($user_info); // echo '</pre>';
只要你得到了code,就可以使用上面的方法了,不过这方法有点老,建议用最新的.
获取到用户信息之后就可以直接保存到数据库了,剩下怎么操作就看你自己了
发表评论: