• 多个域名使用一个微信公众号授权


    应用服务器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);
    

      

  • 相关阅读:
    使用Acctinfo.dll了解更多AD用户帐号信息
    vue elementUI之Form表单 验证
    vue element-ui 表格筛选,点击筛选数据
    使用Bootstrap + Vue.js实现 添加删除数据
    CSS3过渡效果 兼容IE6、IE7、IE8
    使用Ajax、json实现京东购物车结算界面的数据交互
    JavaScript面向对象,闭包内存图,闭包和作用域
    实现一个宽和高都是100像素的div可以用鼠标拖拽移动的效果
    JavaScript鼠标事件,点击鼠标右键,弹出div
    javascript sort排序
  • 原文地址:https://www.cnblogs.com/mracale/p/9037188.html
Copyright © 2020-2023  润新知