php 微信登陆unionid,微信扫码登陆/微信公交号 登录PHP 自适应 UnionID统一用户

标签:php<?php

namespace Topxia\Component\OAuthClient;

/**

* 微信扫码登陆

* Enter description here ...

* @author Administrator

*

*/

class WeixinOAuthClient extends AbstractOAuthClient

{

public $tokenURL;

public $authorizeURL;

public $userURL;

public $scope = ‘‘;

public $app_key;

public $app_secret;

public $display = ‘‘;

public $graphURL = ‘‘;

public $token = array();

public $meth = array();

public $post_login = array();

public $post_token = array();

public $post_msg = array();

public function getAuthorizeUrl($callbackUrl)

{

$state = md5(time()+rand(0,9999));

$_SESSION[‘weixin_state‘] = $state;

$url="";

if(strpos($_SERVER[‘HTTP_USER_AGENT‘], ‘MicroMessenger‘) !== false)

{

/**

* 公众号登录

* Enter description here ...

* @var unknown_type

*/

$this->config[‘key‘]="wx";//用户 微信公交号登录的 APPID

$url = "https://open.weixin.qq.com/connect/oauth2/authorize";

$url .="?appid=".$this->config[‘key‘]."&redirect_uri=".urlencode($callbackUrl)."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";

}

else

{

/**

* 微信网页授权登录

* Enter description here ...

* @var unknown_type

*/

$this->authorizeURL = ‘https://open.weixin.qq.com/connect/qrconnect‘;

$this->app_key = $this->config[‘key‘];

$this->app_secret = $this->config[‘secret‘];

$this->parameter = array(

‘response_type‘ => ‘code‘,

‘scope‘ => ‘snsapi_login‘,

‘state‘ => $state,

);

$url = $this->authorizeURL.‘?‘;

$url .= ‘appid=‘.$this->app_key;

if(!empty($callbackUrl))

{

$this->parameter[‘redirect_uri‘] = $callbackUrl;

}

$url .= ‘&‘.http_build_query($this->parameter);

}

return $url;

}

public function getAccessToken($code, $callbackUrl)

{

if(strpos($_SERVER[‘HTTP_USER_AGENT‘], ‘MicroMessenger‘) !== false)

{

/**

* 公众号登录

* Enter description here ...

* @var unknown_type

*/

$APPID="wx";//用户 微信公交号登录的 APPID

$SCRETID="200049";//用户 微信公交号登录的 SCRETID

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$APPID."&secret=".$SCRETID."&code=".$code."&grant_type=authorization_code";

$re = $this->curl_get_contents1($url);

$rearr = json_decode($re,true);

$openid = $rearr[‘openid‘];

$url3 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$SCRETID;

$re3 = $this->curl_get_contents1($url3);

$re3arr = json_decode($re3,true);

//print_r($re3arr);exit;

$token1[‘openid1‘]=$openid;

$token1[‘token‘]=$re3arr[‘access_token‘];

$unionid=$this->getunionid($token1);

//$_SESSION[‘openid‘] = $openid;

$token = array(

‘userId‘ => $unionid,

‘token‘ => $re3arr[‘access_token‘],

‘openid‘ => $unionid,

‘openid1‘ => $openid,

// ‘refresh_token‘ => $rawToken[‘refresh_token‘],

);

return $token;

}

else

{

$url = sprintf(‘https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code‘,$this->config[‘key‘],$this->config[‘secret‘],$code);

$re = $this->curl_get_contents1($url);

$rawToken = json_decode($re,true);

if($rawToken[‘errcode‘]){

echo "错误提示:".$data;

exit;

}

//$_SESSION[‘openid‘] = $rawToken[‘openid‘];

$token1[‘openid1‘]=$rawToken[‘openid‘];

$token1[‘token‘]=$rawToken[‘access_token‘];

$unionid=$this->getunionid($token1);

$token = array(

‘userId‘ => $unionid,

‘token‘ => $rawToken[‘access_token‘],

‘openid‘ => $unionid,

‘openid1‘ => $rawToken[‘openid‘],

‘refresh_token‘ => $rawToken[‘refresh_token‘],

);

return $token;

}

}

/**

* 获取unionid 统一微信登录用户

* Enter description here ...

* @param unknown_type $token

*/

public function getunionid($token)

{

$userInfo=array();

if(strpos($_SERVER[‘HTTP_USER_AGENT‘], ‘MicroMessenger‘) !== false)

{

/**

* 公众号登录

* Enter description here ...

* @var unknown_type

*/

header("Content-type: text/html; charset=utf-8");

$url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token[‘token‘]."&openid=".$token[‘openid1‘]."&lang=zh_CN";

$re2 = $this->curl_get_contents1($url2);

$userInfo = json_decode($re2,true);

}

else

{

$params = array();

$params[‘access_token‘] = $token[‘token‘];

$params[‘openid‘] = $token[‘openid1‘];

$url=‘https://api.weixin.qq.com/sns/userinfo‘;

$url .="?access_token=".$params[‘access_token‘]."&openid=".$params[‘openid‘]."&lang=zh_CN";

$re = $this->curl_get_contents1($url);

$userInfo = json_decode($re,true);

}

return $userInfo[‘unionid‘];

}

public function getUserInfo($token)

{

$userInfo=array();

if(strpos($_SERVER[‘HTTP_USER_AGENT‘], ‘MicroMessenger‘) !== false)

{

/**

* 公众号登录

* Enter description here ...

* @var unknown_type

*/

header("Content-type: text/html; charset=utf-8");

$url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token[‘token‘]."&openid=".$token[‘openid1‘]."&lang=zh_CN";

$re2 = $this->curl_get_contents1($url2);

$userInfo = json_decode($re2,true);

}

else

{

$params = array();

$params[‘access_token‘] = $token[‘token‘];

$params[‘openid‘] = $token[‘openid1‘];

$url=‘https://api.weixin.qq.com/sns/userinfo‘;

$url .="?access_token=".$params[‘access_token‘]."&openid=".$params[‘openid‘]."&lang=zh_CN";

$re = $this->curl_get_contents1($url);

$userInfo = json_decode($re,true);

//$this->checkError($userInfo);

}

//print_r($userInfo);exit;

return $this->convertUserInfo($userInfo);

}

protected function convertUserInfo($rawUserInfo)

{

$info = array();

//print_r($rawUserInfo);

$info[‘name‘] = $rawUserInfo[‘nickname‘];

$info[‘sex‘] = $rawUserInfo[‘sex‘];

$info[‘id‘] = $rawUserInfo[‘unionid‘];

$info[‘smallAvatar‘] = $rawUserInfo[‘headimgurl‘];

return $info;

}

//var_dump($openid);

function curl_get_contents1($url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_TIMEOUT, 2);

curl_setopt($ch, CURLOPT_USERAGENT, "IE 6.0");

curl_setopt($ch, CURLOPT_REFERER, "");

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$r = curl_exec($ch);

curl_close($ch);

return $r;

}

private function checkError($userInfo)

{

if (!array_key_exists(‘error_code‘, $userInfo)) {

return ;

}

if ($userInfo[‘error_code‘] == ‘21321‘) {

throw new \Exception(‘unaudited‘);

}

throw new \Exception($userInfo[‘error‘]);

}

}

标签:php

原文:http://3037704620.blog.51cto.com/12162790/1902754

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值