微信oauth2.0和自定義菜單攜帶openid

做了一個普通的首頁網站是通過賬號密碼來進行登錄的,看到別的網站可以通過在微信中直接打開而不用輸入賬號密碼,且是和微信綁定的,雖然不知道這是怎麼實現的,自己想了下通過點擊微信的自定義菜單時候攜帶上自己的openid,在用戶表找下對應的openid,如果存在就自動創建用戶session如果不存在則認爲未登錄,不知道這種思路對不對。


微信授權獲取用戶openid和用戶基本信息,頁面獲取openid主要用到微信的oAuth2.0網頁授權,且具備該權限,認證的服務號。


授權有兩種方式:

1、無需用戶確認(悄悄模式) snsapi_base 

2、需要用戶確認 snsapi_userinfo


授權步驟:

1、通過頁面點擊"click"事件 觸發或者放入自定義菜單url(貌似不能通過curl觸發不然CODE好像沒法傳給回調URL接收)兩種授權方式的URL鏈接給回調地址傳遞CODE;

2、通過CODE、APPID、APPSECRET獲取用戶access_token憑證(和以往access_token不是一回事兒 只是名字一樣而已);

3、通過access_token獲取用戶openid (如果是snsapi_base模式到此就已經完成)

4、通過access_token、APPID獲取用戶基本信息(snsapi_userinfo模式到此完成)


封裝獲取用戶openid和userinfo信息類:

<?php
/**
* oAuth2.0網頁授權
*
* 1、具有微信的oAuth2.0權限 認證的服務號
* 2、配置oAuth2.0的回調域名 不帶http or https 
*  配置回調域名注意:www.sxfenglei.com 和 sxfenglei.com是2個不同的域 www.sxfenglei.com並不屬於sxfenglei.com  
* @author sxfenglei
* @email [email protected]
*
* 事例:
* require_once 'WxOauth.class.php';
* define('APPID','xxxxxx');
* define('SECRET','xxxxxx');
* define('CODE',$_GET['code']);
* $wx = new WxOauth(APPID,SECRET,CODE);
* //$res = $wx->getOpenid();
* $res = $wx->getUserinfo();
* var_dump($res);
*/
class WxOauth{
	private $appid;
	private $appsecret; 
	private $code;
	private $access_token;
	private $openid; 
	private $userinfo;
 
	/**
	* 初始化 需要APPID 、 APPSECRET 和 CODE
	* 特別注意這個CODE是通過頁面“點擊”觸發 get請求後傳遞給回調的 好像不能通過curl get觸發
	*/
	public function __construct($appid,$appsecret,$code){
		if(empty($appid) || empty($appsecret)||empty($code)){
			die('init fail');
		}
		//appid and appsecret
		$this->appid = $appid;
		$this->appsecret = $appsecret;
		
		//code
		if(empty($code)){ 
			die('parameter error');
		}
		$this->code = $code;

		//cURL
		require_once 'function.class.php';
	}
 
	/*
	* post 獲取openid
	* 獲取 CODE有兩種方式:
	* snsapi_base不用用戶“確認”但只能獲取openid
	* snsapi_userinfo 需要用戶“確認”但可以獲取openid 和 用戶信息
	*/
	public function getOpenid($scope="snsapi_base"){   
		//code換取access_token
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?";
		$arr = array(
			'appid'=>$this->appid,
			'secret'=>$this->appsecret,
			'code'=>$this->code,
			'grant_type'=>'authorization_code'
		);

		$dataArr = json_decode(postCurl($url,$arr),true);
		$this->access_token = $dataArr['access_token'];
		$this->openid = $dataArr['openid'];
		if($scope=="snsapi_base"){
			return $dataArr['openid'];  
		}else{
			return $dataArr;  
		}
	}

	/**
	* get 獲取userinfo
	*/
	public function getUserinfo(){
		//code獲取access_token票據
		$res = $this->getOpenid("snsapi_userinfo");
		$this->access_token = $res['access_token'];
		//access_token換取用戶信息
		/*
		$url = "https://api.weixin.qq.com/sns/userinfo?";
		$postArr = array(
			'access_token'=>$this->access_token,
			'openid'=>$this->openid,
			'lang'=>'zh_CN'
		); 
		$this->userinfo = json_decode(postCurl($url,$postArr),true);
		*/
		$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$this->openid."&lang=zh_CN";
		$this->userinfo = json_decode(getCurl($url),true);
		return $this->userinfo;
	}

	/**
	* get 驗證access_token是否有效
	*/
	public function isValidAccessToken($access_token){
		if(empty($access_token)){
			die('The access_token cannot be empty');
		}
		$url = "https://api.weixin.qq.com/sns/auth?access_token=".$access_token."&openid=".$this->openid; 
		$res = json_decode(getCurl($url),true);
		if($res['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}

	/**
	* post 刷新access_token
	*/
	public function refreshAccessToken($refresh_token){
		if(empty($refresh_token)){
			die('The refresh_token cannot be empty');
		}
		//$url = "appid=".$this->openid."&grant_type=refresh_token&refresh_token=".$refresh_token; 
		//$res = json_decode(getCurl($url),true);
 
		$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?";
		$arr = array(
			'appid'=>$this->appid,
			'grant_type'=>'refresh_token',
			'refresh_token'=>$refresh_token
		);

		$res = json_decode(postCurl($url,$arr),true);
 
		return $res; 
	}
} 


?>

用法:

首先通過點擊超鏈接傳遞CODE給回調頁 來獲取CODE 不能用curl因爲其中包含有回調URL

<?php
//引入類
require_once 'WxOauth.class.php';

//該回調頁 獲取code
if(!isset($_GET['code']) && empty($_GET['code'])){
	echo '用戶未授權';
	exit;
} 
define('APPID','填寫你自己的APPID');  //需要認證的服務號
define('SECRET','填寫你自己的APPSECRET');
define('CODE',$_GET['code']);
$wx = new WxOauth(APPID,SECRET,CODE);
//$res = $wx->getOpenid();  //獲取openid
$res = $wx->getUserinfo();  //獲取userinfo
var_dump($res);
?>




頁面獲取openid主要用到微信的oAuth2.0網頁授權,且具備該權限,認證的服務號。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章