企业公众号-网页授权
//网页授权获取用户信息
public function wxapi()
{
/*格式
js--location.href='http://xxx/api/wxapi/wxapi?redirect_uri='+urlencode('http://xxx/api/wxapi/wxapi')+'&backurl=urlencode(返回到前台地址)';
*/
//判断请求方式
if ($this->isHttps()) {
$protocol = 'https';
} else {
$protocol = 'http';
}
//初始化变量
$appid = 'xxx';
$secret= 'xxx';
$scope = 'snsapi_userinfo';
$state = 'STATE';
$code = '';
$redirect_uri = $protocol . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$device = '';
$authUrl='https://open.weixin.qq.com/connect/oauth2/authorize';
$back_url='';
//回调地址
if (isset($_GET['back_url'])) {
$back_url = urldecode($_GET['back_url']);
}
//code返回本页面
if(isset($_GET['redirect_uri'])){
$redirect_uri=urldecode($_GET['redirect_uri']);
}
//appid
if (isset($_GET['appid'])) {
$appid = $_GET['appid'];
}
if (isset($_GET['secret'])) {
$secret = $_GET['secret'];
}
if (isset($_GET['state'])) {
$state = $_GET['state'];
}
//临时令牌
if (isset($_GET['code'])) {
$code = $_GET['code'];
}
//授权方式
if (isset($_GET['scope'])) {
$scope = $_GET['scope'];
}
//获取用户信息
if(!empty($_GET['getinfo']) && $_GET['getinfo']=="getinfo"){
//https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
//获取网页access_token,
$access_token = file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$appid}&corpsecret=$secret");
$token=json_decode($access_token,1);
if(isset($token['errcode']) && $token['errcode']!=0){
$this->content=$token;
$this->errorInfo(2);
}
$arr=file_get_contents('https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$token['access_token'].'&code='.$code);
$arr=json_decode($arr,1);
if(isset($arr['UserId'])){
$arr=file_get_contents('https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='.$token['access_token'].'&userid='.$arr['UserId']);
$arr=json_decode($arr,1);
}
$this->content=$arr;
$this->errorInfo(0);
}
if (empty($code)) {
//访问地址
if (isset($_GET['authUrl'])) {
$authUrl = $_GET['authUrl'];
}
//回调地址【微信中间件地址】
$options = [
$authUrl,
'?appid=' . $appid,
'&redirect_uri=' . urlencode($redirect_uri),
'&response_type=code',
'&scope=' . $scope,
'&agentid=47',
'&state=' . $state,
'#wechat_redirect',
];
//把redirect_uri先写到cookie
setcookie('redirect_uri', urlencode($back_url));
header('Location: ' . implode('', $options));echo 123;die;
} else {
//查回调地址
if (isset($_COOKIE['redirect_uri'])) {
$back_url = urldecode($_COOKIE['redirect_uri']);
}
if(empty($back_url)){
die('参数 back_url 不能为空,请重新访问');
}
if(strpos($back_url,'/#')){
$aa=(strpos($back_url,'/#')-1);
if($back_url[$aa]=='/'){
$back_url=substr($back_url,0,$aa).substr($back_url,($aa+1));;
};
}
//返回code
//跳转到回调页面
header('Location: ' . implode('', [
$back_url,
strpos($back_url, '?') ? '&' : '?',
'code=' . $code,
]));echo 456;die;
}
}
private function isHttps()
{
if (!isset($_SERVER['HTTPS'])) {
return false;
}
//判断服务器类型
if ($_SERVER['HTTPS'] === 1) {
//Apache
return true;
} elseif ($_SERVER['HTTPS'] === 'on') {
//IIS
return true;
} elseif ($_SERVER['SERVER_PORT'] == 443) {
//其他
return true;
}
return false;
}
来源:https://blog.csdn.net/sll9711/article/details/99641069