工作上需要做个微信登录,之前没有经验,自己摸索,结果遇到了一些坑,当然是解决了,但是有一个坑好无语,就是回调地址不能有http://,结果链接里的redirect_uri就要加上,不然就报参数错误。网上查找了好多redirect_uri参数错误的答案,竟然没人提到这点,害我百思不解,我也是醉了。
首先是微信网页授权的官方文档,主要是依靠这个来完成微信登录功能
首先要有一个服务号,有appid,appsecret,登录公众平台后填好回调网址,有这三样就没什么问题了。
下载一个微信开发者工具,成为开发者,会方便开发,当然没有也没什么问题,但是你就无法在电脑上直接观察调试结果了。
这是登录授权链接,只需要填写appid和redirect_uri。注意redirect_uri要加http://,当然也有可能是我这个回调网址特殊。snsapi_userinfo是获取用户所有信息,具体参考官方说明,就是网页授权链接。
不出意外点击这个链接会出现授权登录页面,点登录就会跳转到回调地址。
登录后的地址类似:
得到code可以获取access_token和opendi,code和access_token都可以看成令牌,opendi就是得到用户信息的关键。
插句话,要验证能否顺利得到access_token,也顺便验证你的appid和appsecret是否没问题,可以到这个网址验证,万一之后出问题可以排除appid和appsecret的错误。
这是获取access_token的链接:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret
直接上代码,不出问题可以得到用户信息:
<?php
class weixinclass
{
var $appid;
var $appsecret;
//构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid){
$this->appid = $appid;
}
if($appsecret){
$this->appsecret = $appsecret;
}
$this->access_token = "";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->https_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
}
//获取用户基本信息
public function get_user_info($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->https_request($url);
return json_decode($res, true);
}
//https请求
public function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
<?php
header('content-type:text/html;charset=utf-8');
class OauthAction extends Action {
public function oauth2(){
require_once 'weixinclass.php';
$appid = '填appid';
$appsecret = '填appsecret ';
$weixin=new weixinclass($appid,$appsecret);
if (isset($_GET['code'])){
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$_GET['code']."&grant_type=authorization_code";
$res = $weixin->https_request($url);
$res=(json_decode($res, true));
$row=$weixin->get_user_info($res['openid']);
if ($row['openid']) {
//这里写上逻辑,存入cookie,数据库等操作
echo "<pre>";
print_r($row);die;
cookie('weixin',$row['openid'],25920);
}else{
$this->error('授权出错,请重新授权!');
}
}else{
echo "NO CODE";
}
$this->display();
}
}
上面的代码也是参考一些别人的内容,经测试没问题。然后打印出来的信息类似这样:
Array
(
[subscribe] =>
[openid] =>
[nickname] => nobody
[sex] => 1
[language] => zh_CN
[city] =>
[province] =>
[country] => 亚美尼亚
[headimgurl] =>
[unionid] =>
[remark] =>
[groupid] => 0
[tagid_list] => Array
(
)
)
end.
转载于:https://blog.51cto.com/1105190775/1896167
6599

被折叠的 条评论
为什么被折叠?



