微信授权登录类

<?php
/**
* 微信授权登录类
* User: chenzhijian
* Date: 2016/3/24
* Time: 14:26
*/
namespace WxLogin;

class Wxlogin{

//AppId(应用ID)
private $appid = "*******";
//AppSecret(应用密钥)
private $appSecret = "*********";
//code (同意授权后的code)
private $code = "";
//access_token(网页授权后获取)
private $accessToken = "";
//返回类型
private $responseType='code';
//scope 返回用户类型字段 nsapi_base | snsapi_userinfo
private $scope = "snsapi_userinfo";
//请求微信url
private $url = "https://open.weixin.qq.com/connect/oauth2/authorize";
//请求微信api_url
private $apiUrl = "https://api.weixin.qq.com/sns";
//回调地址
private $redireUri = "http://xxx.com/callback";

public function __construct(){

}
/*
* 获取code
* explame https://open.weixin.qq.com/connect/oauth2/authorize?appid=****&redirect_uri=http://www.xxx.com&response_type=code&scope=snsapi_userinfo&state=233232#wechat_redirect
* return http://m.mkf.com/?code=0216ec79275a460d201e0cee2631f70X&state=KlhsiE%23wechat_redirect
*/
public function getCode(){
$param = array(
'appid'=> $this->appid, //AppId(应用ID)
'redirect_uri'=> $this->redireUri, //授权后重定向的回调链接地址,请使用urlencode对链接进行处理
'response_type'=> $this->responseType, //返回类型
'scope'=> $this->scope, //scope 返回用户类型字段
'state' => $this->randString(6).'#wechat_redirect' //重定向后会带上state参数
);
$param =http_build_query($param);
$url = $this->url.'?'.$param;
redirect($url);
}
/*
* explame https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxx&secret=xxxx&code=0216ec79275a460d201e0cee2631f70X&grant_type=authorization_code
* 获取access_token
* @return json access_token expires_in refresh_token openid scope
*/
public function getAccessToken($code){
$code = $code ? $code : trim($_GET['code']); //code
$param = array(
'appid'=> $this->appid, //AppId(应用ID)
'secret'=> $this->appSecret, //AppSecret(应用密钥)
'code'=> $code, //code
'grant_type'=> 'authorization_code', //authorization_code
);
//请求api地址
$url = $this->apiUrl.'/oauth2/access_token';
$result = $this->getCurl($url,$param);
return $result;
}

/*
* example https://api.weixin.qq.com/sns/userinfo?access_token=xxx-xx&openid=xxx
* 获取微信用户信息
* @return array
*/
public function getUserInfo($access_token,$openid){
$param = array(
'access_token'=> $access_token, //网页授权凭证
'openid'=> $openid //用户的唯一标识
);
//请求api地址
$url = $this->apiUrl.'/userinfo';
$result = $this->getCurl($url,$param);
return $result;

}
/**
* explame https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=xxx&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
* 获取access_token
* @return json access_token expires_in refresh_token openid scope
*/
public function refreshAccessToken($openid,$refresh_token){
$refresh_token = trim($refresh_token);
$param = array(
'appid'=>$openid,
'grant_type'=>'refresh_token',
'refresh_token'=>$refresh_token
);
//请求api地址
$url = $this->apiUrl.'/oauth2/refresh_token';
$result = $this->getCurl($url,$param);
return $result;
}
//随机数
public function randString($len=6){
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z"
);
$charsLen = count($chars) - 1;
// 将数组打乱
shuffle($chars);
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}


//curl调用
public function getCurl($domain='',$param){
$param =http_build_query($param);
$url = $domain.'?'.$param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result,true);
return $result;
}




}
posted @ 2016-04-12 14:40  php点点滴滴  阅读(501)  评论(0编辑  收藏  举报