微信授权登录获取openId和unionId
发表时间: 2016-10-14 14:10:03 | 浏览次数:
小编今天在写完上一篇文章的时候,发现微信登录可直接获取到unionId了,不需要通过openId获取了,感觉上一篇文章又白写了。。。那应该是小编昨天将公众平台和开放平台进行了绑定,所以返回的数据上带有unionId,不过只是小编的猜测,还没证实,如果是这样的话,那上一篇文章就没白写,哈哈。
大家按照自己的需求使用相应的代码吧。
未绑定之前授权登录返回的数据
Array( [access_token] => 4A5T0uMIxn6jTZuRqkIqqN3gvgZ44oowfAlg5idlsayHR2KvNYpaXD0z_T8IooStg09k_LwczeoVuFo1D2ROINSDmJxVv5jhqLlFOsRahaI [expires_in] => 7200 [refresh_token] => Ke6xysXtrLA90gCSIvyI1UdTNpuTzpjoAF8zsSO92QGGyM8nuB-zOpxC_G39i-feHvzENf2a2V0cJrh7khtwkw5KVWT3mvthVyEs_l-N9pw [openid] => o4Qp7jutSavCjhydjhPadfasa15g [scope] => snsapi_userinfo )
绑定之后授权登录返回的数据
Array( [access_token] => ieUCVH5t2GlMP_oLJduw2mIcNrWNrdMlGMNeLKscazRkFsT0dQbkjQ7pMuanVv4UWJ__oFG3dTfe1LeUC7Phw0BR-uoZ3ewSUKnsioj9Q2U [expires_in] => 7200 [refresh_token] => JE2jcXDmSGlzZxErTEvezqpURZ-llh7MM_bMIoRIjU3hfGc85SgK7Bp6eO25RQd4ctNIb3aV35aPQeMaNb0AyQo2Sl3WPPixbOsGDIe6gjo [openid] => oSKzJvk9Uasfasdfylk3b-frjuo [scope] => snsapi_userinfo [unionid] => ozv7awqyc4jQqwerqwervOqQM )
WeChat.class.php
// +----------------------------------------------------------------------
// | 微信公众平台开发
// | 1.发送模板短信
// | 2.获取微信用户信息
// | 3.微信授权登录获取openId和unionId
// | 4...
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lrfun.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: lrfun <admin@lrfun.com>
// +----------------------------------------------------------------------
// | Date: 2016-10-14
// +----------------------------------------------------------------------
Class WeChat{
protected $appid;
protected $secret;
protected $accessToken;
function __construct(){
$this->appid = "xxx";
$this->secret = "xxx";
$this->accessToken = $this->getAccessToken();
}
/***
* 获取access_token
* @return
**/
private function getAccessToken(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
$res = json_decode($this->httpRequest($url),true);
//$access_token = $res['access_token'];
return $res['access_token'];
}
/***
* POST或GET请求
* @url 请求url
* @data POST数据
* @return
**/
private function httpRequest($url, $data = ""){
$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)){ //判断是否为POST请求
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;
}
/***
* 获取openID或unionId
* @code 微信授权返回的code
* @return
**/
public function getOpenIdOrUnionId($code){
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
$data = $this->httpRequest($url);
return $data;
}
/***
* 发送模板短信
* @data 请求数据
* @return
**/
public function sendTemplateMessage($data = ""){
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->accessToken;
$result = $this->httpRequest($url, $data);
return $result;
}
/***
* 通过openId获取用户信息
* @openId
* @return
**/
public function getUserInfo($openId){
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->accessToken."&openid=".$openId."&lang=zh_CN";
$data = $this->httpRequest($url);
return $data;
}
}
(转载请注明:转自http://www.lrfun.com)
上一篇:微信公众平台|通过openId获取用户信息(unionId)
下一篇:PayPal支付教程