多個域名使用一個微信公眾號授權


應用服務器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);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM