最近做的一个商城项目中,要用到发货和收货人地址的功能,上面要求要用微信PC扫码授权登陆;自己试着用研究了好久写了一个,但是发现有好多兼容问题,具体实现步骤如下.微信PC扫码授权登陆 php简单示例代码
首先须要申请,必须企业,个体工商户,媒体等
申请地址 https://open.weixin.qq.com/
演示参考下这个:http://t.cn/ROuy90K
1、首先到微信开放平台申请https://open.weixin.qq.com/ 获取到appid和APPSECRET,前台显示页面如下
前台完整代码如下:
Html代码 收藏代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<span id="login_container"></span>
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<script>
var obj = new WxLogin({
id: "login_container",
appid: "wxed782be999f86e0e",
scope: "snsapi_login",
redirect_uri: encodeURIComponent("http://" + window.location.host + "/login.php"),
state: Math.ceil(Math.random()*1000),
style: "black",
href: ""});
</script>
</body>
</html>
2、服务器端完整代码如下:
Php代码
/*
require_once(‘weixin.class.php’);
$weixin = new class_weixin();
*/
define('APPID', "wx19ba77624e083e08");
define('APPSECRET', "c1a56a5c4247dd44c320c9719c5ceb90");
class class_weixin
{
var $appid = APPID;
var $appsecret = APPSECRET;
//构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//扫码登录不需要该Access Token, 语义理解需要
//1. 本地写入
$res = file_get_contents('access_token.json');
$result = json_decode($res, true);
$this->expires_time = $result["expires_time"];
$this->access_token = $result["access_token"];
if (time() > ($this->expires_time + 3600)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$this->expires_time = time();
file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');
}
}
/*
* PART1 网站应用
*/
/*
header(“Content-type: text/html; charset=utf-8”);
require_once(‘wxopen.class.php’);
w
e
i
x
i
n
=
n
e
w
c
l
a
s
s
w
e
i
x
i
n
(
)
;
i
f
(
!
i
s
s
e
t
(
weixin = new class_weixin(); if (!isset(
weixin=newclassweixin();if(!isset(_GET[“code”])){
r
e
d
i
r
e
c
t
u
r
l
=
′
h
t
t
p
:
/
/
′
.
redirect_url = 'http://'.
redirecturl=′http://′._SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’];
$jumpurl =
w
e
i
x
i
n
−
>
q
r
c
o
n
n
e
c
t
(
weixin->qrconnect(
weixin−>qrconnect(redirect_url, “snsapi_login”, “123”);
Header(“Location: $jumpurl”);
}else{
$oauth2_info =
w
e
i
x
i
n
−
>
o
a
u
t
h
2
a
c
c
e
s
s
t
o
k
e
n
(
weixin->oauth2_access_token(
weixin−>oauth2accesstoken(_GET[“code”]);
$userinfo =
w
e
i
x
i
n
−
>
o
a
u
t
h
2
g
e
t
u
s
e
r
i
n
f
o
(
weixin->oauth2_get_user_info(
weixin−>oauth2getuserinfo(oauth2_info[‘access_token’],
o
a
u
t
h
2
i
n
f
o
[
′
o
p
e
n
i
d
′
]
)
;
v
a
r
d
u
m
p
(
oauth2_info['openid']); var_dump(
oauth2info[′openid′]);vardump(userinfo);
}
*/
//生成扫码登录的URL
public function qrconnect($redirect_url, $scope, $state = NULL)
{
u
r
l
=
"
h
t
t
p
s
:
/
/
o
p
e
n
.
w
e
i
x
i
n
.
q
q
.
c
o
m
/
c
o
n
n
e
c
t
/
q
r
c
o
n
n
e
c
t
?
a
p
p
i
d
=
"
.
url = "https://open.weixin.qq.com/connect/qrconnect?appid=".
url="https://open.weixin.qq.com/connect/qrconnect?appid=".this->appid."&redirect_uri=".urlencode(KaTeX parse error: Expected 'EOF', got '&' at position 16: redirect_url)."&̲response_type=c…scope."&state=".$state."#wechat_redirect";
return $url;
}
//生成OAuth2的Access Token
public function oauth2_access_token($code)
{
u
r
l
=
"
h
t
t
p
s
:
/
/
a
p
i
.
w
e
i
x
i
n
.
q
q
.
c
o
m
/
s
n
s
/
o
a
u
t
h
2
/
a
c
c
e
s
s
t
o
k
e
n
?
a
p
p
i
d
=
"
.
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".
url="https://api.weixin.qq.com/sns/oauth2/accesstoken?appid=".this->appid."&secret=".KaTeX parse error: Expected 'EOF', got '&' at position 18: …is->appsecret."&̲code=".code."&grant_type=authorization_code";
$res =
t
h
i
s
−
>
h
t
t
p
r
e
q
u
e
s
t
(
this->http_request(
this−>httprequest(url);
return json_decode($res, true);
}
//获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
public function oauth2_get_user_info($access_token, $openid)
{
u
r
l
=
"
h
t
t
p
s
:
/
/
a
p
i
.
w
e
i
x
i
n
.
q
q
.
c
o
m
/
s
n
s
/
u
s
e
r
i
n
f
o
?
a
c
c
e
s
s
t
o
k
e
n
=
"
.
url = "https://api.weixin.qq.com/sns/userinfo?access_token=".
url="https://api.weixin.qq.com/sns/userinfo?accesstoken=".access_token."&openid=".$openid."&lang=zh_CN";
$res =
t
h
i
s
−
>
h
t
t
p
r
e
q
u
e
s
t
(
this->http_request(
this−>httprequest(url);
return json_decode($res, true);
}