网页授权
应用 A :a.com
其他应用:b.com,c.com ...
其他应用 通过 返回的唯一标识 来调用主应用的接口 获取信息
1) 跳转A应用 url?redirect_uri=外部uri
2) 缓存外部uri
3) 跳转微信授权url?redirect_uri=当前uri
4) 微信授权完成 获取code
根据code 获取到用户所有信息 并用
key(code,userinfo) 格式缓存
5) 跳回 外部uri 并携带参数 code
6) 外部应用 根据code 向A应用请求接口
其他应用:--------------------------------------------------
其他应用 通过 code 直接从微信拉取用户信息
1) 跳转A应用 url?redirect_uri=外部uri
2) 缓存外部uri
3) 跳转微信授权url?redirect_uri=当前uri
4) 微信授权完成 获取code
5) 跳回 外部uri 并携带参数 code
6) 根据 code 从微信拉取用户信息
主应用 A----------------------------------------------
其他应用通过A 提供的ticket接口 获取ticket 并组建jssdk
其他应用通过A 提供的jssdk 接口 直接获取
应用 A :a.com
其他应用:b.com,c.com ...
一、网页授权
注意授权域名 写主域名A 的域名地址
1. 方式一
主应用 A 获取用户信息 并缓存其他应用 通过 返回的唯一标识 来调用主应用的接口 获取信息
1) 跳转A应用 url?redirect_uri=外部uri
2) 缓存外部uri
3) 跳转微信授权url?redirect_uri=当前uri
4) 微信授权完成 获取code
根据code 获取到用户所有信息 并用
key(code,userinfo) 格式缓存
5) 跳回 外部uri 并携带参数 code
6) 外部应用 根据code 向A应用请求接口
取到用户信息
主应用 A---------------------------------------
/**
* 入口文件
*/
public function wysq(){
$this->session->set_userdata('redirect_uri',$this->input->get('redirect_uri'));
$array = array(
'appid' =>$this->_appid,
'redirect_uri' =>'http://'.$_SERVER['HTTP_HOST'].'/home/api/oauth',
'response_type' =>'code',
'scope' =>'snsapi_userinfo',//snsapi_base ,snsapi_userinfo
'state' =>'state'
);
$list = http_build_query($array);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'.$list.'#wechat_redirect';
echo '<script> location.href="'.$url.'"</script>';exit;
}
public function oauth(){
$code = $this->input->get('code');
if($code){
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$userinfo = $this->curl->simple_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->_appid&secret=$this->_appsecret&code=$code&grant_type=authorization_code");
$user = json_decode($userinfo);
$access_token = $user->access_token;
$openid = $user->openid;
//这里判断用户是否存在啊
if($openid !== ''){
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$baseinfo = $this->curl->simple_get('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
$info = json_decode($baseinfo);
if(empty($info)){
die('微信返回有误!!!');
}
//coding here... 将info 存入缓存 key $code : $info
$gouri = $this->session->userdata('redirect_uri');
$gouri = stripos($gouri, '?') === false ? $gouri.'?code='.$code:$gouri.'&code='.$code;
header("Location:".$gouri);
exit;
}else{
die('微信返回有误!!!');
}
}else{
die('微信返回有误!!!');
}
}
//获取用户信息接口
public function getinfo(){
header("Content-type: application/json");
$code = $this->input->get('code');
if($code){
//coding here... 这里根据code 拿到用户信息 并删除缓存
if($userinfo){
echo json_encode(array('status'=>1,'msg'=>'信息获取成功!','userinfo'=>$userinfo));
exit;
}
echo json_encode(array('status'=>-2,'msg'=>'code已失效,请重新授权!'));
exit;
}else{
echo json_encode(array('status'=>-1,'msg'=>'code不存在!'));
exit;
}
}
其他应用:--------------------------------------------------
//授权入口
public function other_auth(){
$userinfo = $this->session->userdata('userinfo');
if(!$userinfo){
$redirect_uri = '当前url'.'/getuser';
$uri = 'A应用地址'."?redirect_uri=".$redirect_uri;
header("Location:".$uri);
exit;
}
}
public function getuser(){
$code = $this->input->get('code');
$userinfo = $this->curl->simple_get('A应用地址'."/getinfo",array('code'=>$code));
//此处即取到用户信息...
}
2. 方式二
主应用 A 只跳转从微信取到 code其他应用 通过 code 直接从微信拉取用户信息
1) 跳转A应用 url?redirect_uri=外部uri
2) 缓存外部uri
3) 跳转微信授权url?redirect_uri=当前uri
4) 微信授权完成 获取code
5) 跳回 外部uri 并携带参数 code
6) 根据 code 从微信拉取用户信息
主应用 A----------------------------------------------
/**
* 入口文件
*/
public function wysq(){
$this->session->set_userdata('redirect_uri',$this->input->get('redirect_uri'));
$array = array(
'appid' =>$this->_appid,
'redirect_uri' =>'http://'.$_SERVER['HTTP_HOST'].'/home/api/oauth',
'response_type' =>'code',
'scope' =>'snsapi_userinfo',//snsapi_base ,snsapi_userinfo
'state' =>'state'
);
$list = http_build_query($array);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'.$list.'#wechat_redirect';
echo '<script> location.href="'.$url.'"</script>';exit;
}
public function oauth(){
$code = $this->input->get('code');
if($code){
$gouri = $this->session->userdata('redirect_uri');
$gouri = stripos($gouri, '?') === false ? $gouri.'?code='.$code:$gouri.'&code='.$code;
header("Location:".$gouri);
exit;
}else{
die('微信返回有误!!!');
}
}
其他应用:------------------------------------------
//授权入口
public function other_auth(){
$userinfo = $this->session->userdata('userinfo');
if(!$userinfo){
$redirect_uri = '当前url'.'/getuser';
$uri = 'A应用地址'."?redirect_uri=".$redirect_uri;
header("Location:".$uri);
exit;
}
}
public function getuser(){
$code = $this->input->get('code');
if($code){
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$userinfo = $this->curl->simple_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->_appid&secret=$this->_appsecret&code=$code&grant_type=authorization_code");
$user = json_decode($userinfo);
$access_token = $user->access_token;
$openid = $user->openid;
//这里判断用户是否存在啊
if($openid !== ''){
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$baseinfo = $this->curl->simple_get('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
$info = json_decode($baseinfo);
if(empty($info)){
die('微信返回有误!!!');
}
//coding here... 这里已经获取到用户信息了
exit;
}else{
die('微信返回有误!!!');
}
}else{
die('微信返回有误!!!');
}
}
二、jssdk 获取
(这个最多只能绑定3个域名)
1.方式一
主应用 A 获取ticket其他应用通过A 提供的ticket接口 获取ticket 并组建jssdk
主应用A ---------------------------------------------
//载入时
public function index(){
header("Content-type: application/json");
$token = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt');
$nowtime = time();
if($token){
$str = explode('`',$token);
if($nowtime>=$str[1] or !$str[0]){
$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret;
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get($access_token_url);
$crect = json_decode($src);
$access = $crect->access_token;
$time = time()+7100;
$string = $access.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt',$string);
}else{
$access = $str[0];
}
}else{
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
$crect = json_decode($src);
$access = $crect->access_token;
$time = time()+7100;
$string = $access.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt',$string);
}
$access_token = $access; //获取到基础access_token
//开始获取ticket
$ticket = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt');
if($ticket){
$str = explode('`',$ticket);
if($nowtime>=$str[1] or !$str[0]){
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$access_token_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
$src = $this->curl->simple_get($access_token_ticket_url);
$crect = json_decode($src);
$access_ticket = $crect->ticket;
$time = time()+7100;
$string = $access_ticket.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt',$string);
}else{
$access_ticket = $str[0];
}
}else{
$access_token_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get($access_token_ticket_url);
$crect = json_decode($src);
$access_ticket = $crect->ticket;
$time = time()+7100;
$string = $access_ticket.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt',$string);
}
echo json_encode(array('ticket'=>$access_ticket));
exit;
}
其他应用-------------------------------------------------------
public function getjssdk(){
$access_ticket = ->curl->simple_get('A应用提供的ticket接口地址');;
$data['timestamp'] = $nowtime;
$url = urldecode($_POST['url']);//这里获取url参数有个坑
$data['jsapi_ticket'] = $access_ticket;
$data['nonceStr'] = $this->getRandChar(16);
$data['signature'] = sha1("jsapi_ticket={$data['jsapi_ticket']}&noncestr={$data['nonceStr']}×tamp={$data['timestamp']}&url=$url");
$data['appId'] = $this->_appid;
echo json_encode($data);
exit;
}
2.方式二
主应用 A 组建 jssdk其他应用通过A 提供的jssdk 接口 直接获取
主应用A------------------------------------------------------------------
//载入时
public function index(){
header("Content-type: application/json");
$token = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt');
$nowtime = time();
if($token){
$str = explode('`',$token);
if($nowtime>=$str[1] or !$str[0]){
$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret;
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get($access_token_url);
$crect = json_decode($src);
$access = $crect->access_token;
$time = time()+7100;
$string = $access.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt',$string);
}
else{
$access = $str[0];
}
}
else{
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
$crect = json_decode($src);
$access = $crect->access_token;
$time = time()+7100;
$string = $access.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/token.txt',$string);
}
$access_token = $access; //获取到基础access_token
//开始获取ticket
$ticket = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt');
if($ticket)
{
$str = explode('`',$ticket);
if($nowtime>=$str[1] or !$str[0])
{
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$access_token_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
$src = $this->curl->simple_get($access_token_ticket_url);
$crect = json_decode($src);
$access_ticket = $crect->ticket;
$time = time()+7100;
$string = $access_ticket.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt',$string);
}
else
{
$access_ticket = $str[0];
}
}
else
{
$access_token_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
$this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));
$src = $this->curl->simple_get($access_token_ticket_url);
$crect = json_decode($src);
$access_ticket = $crect->ticket;
$time = time()+7100;
$string = $access_ticket.'`'.$time;
$write = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/ticket.txt',$string);
}
if($this->input->get('gourl'))
{
redirect($this->input->get('gourl'));
}
$data['timestamp'] = $nowtime;
$url = urldecode($_POST['url']);//这里获取url参数有个坑
$data['jsapi_ticket'] = $access_ticket;
$data['nonceStr'] = $this->getRandChar(16);
$data['signature'] = sha1("jsapi_ticket={$data['jsapi_ticket']}&noncestr={$data['nonceStr']}×tamp={$data['timestamp']}&url=$url");
$data['appId'] = $this->_appid;
echo json_encode($data);
exit;
}