多个域名使用一个微信公众号授权
应用服务器client.php
<?php //应用服务器的信息 www.abc.com $appid='xxx'; $secret='xxxx'; if (!isset($_GET['code'])) {//到代理服务器 $client='http://www.abc.net/client.php';//应用服务器地址 $server='http://www.def.com/server.php';//代理服务器地址 $url=$server.'?dl='.urlencode($client); header("location:$url");exit; } $code=trim($_GET['code']); $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $access=file_get_contents($url); $data=json_decode($access,true); $access_token=$data['access_token']; $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN'; $user=file_get_contents($url); var_dump($user); ?>
代理服务器server.php
<?php //代理服务器信息 www.def.com $appid='xxxx'; $secret='xxxx'; if (!isset($_GET['code'])) { //获取需要授权的地址 $dl=trim($_GET['dl']); $server='http://www.def.com/server.php'; $redirect_uri=urlencode($server); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state='.$dl.'#wechat_redirect'; header("location:$url");exit(); } $code=trim($_GET['code']); $state=trim($_GET['state']); $state=$state.'?code='.$code; header("location:$state"); ?>
普通授权获取用户信息
$appid=''; $secret=''; if (!isset($_GET['code'])) { $redirect_uri=urlencode('xxxxxxxxx'); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'; header("location:$url");exit(); } $code=trim($_GET['code']); $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $access=file_get_contents($url); $data=json_decode($access,true); $access_token=$data['access_token']; $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN'; $user=file_get_contents($url); var_dump($user);
静默授权
$appid='xxx'; $secret='xxxxxxxx'; if (!isset($_GET['code'])) { $redirect_uri=urlencode('xxxxxxxxx'); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'; header("location:$url");exit(); } $code=trim($_GET['code']); $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $access=file_get_contents($url); $data=json_decode($access,true); var_dump($data);