1.准备工作 登陆微信开放平台,申请对应的应用,获取AppID,AppSecret;
index.php
//设置redirect_uri
$backUrl=urlEncode("http://www.baidu.com/index.php?act=check");
$smarty->assign('backUrl',$backUrl);
$smarty->display('index.html');
$action=$_REQUEST['act']?$_REQUEST['act']:'';
if ($action == 'check'){
$code=$_GET['code'];
$unionid=checkUid($code);
}
//获取unionid
function checkUid($code){
$data=file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxc7ce8558&secret=87b630c82952rtr2t1ret535807&code=". $code . "&grant_type=authorization_code");
$data=json_decode($data,true);
$unionid=$data['unionid'];
return $unionid;
}
index.html
<a href="https://open.weixin.qq.com/connect/qrconnect?appid=wxc7ce8558&redirect_uri={$backUrl}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect" style="margin-left:15px;" >
<img src="static/picture/Wechat.png" alt="微信登录" style="width: 25px; height: 25px; vertical-align: middle;">
</a>