zoukankan      html  css  js  c++  java
  • 微信授权登录

    第一步点击这个链接获取code 

    <h3><span>其他登录方式</span></h3>     

    <a class="ico_wx" href="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$cfg_weixi_appkey}&redirect_uri={$main_host}/pub/thirdlogin/?type=wechat&response_type=code&scope=snsapi_userinfo&state={$code}#wechat_redirect"><em></em><span>微信</span></a>

    地址例子:

    https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcbee7e85e159396c&redirect_uri=http://txy.handongkeji.com/pub/thirdlogin/?type=wechat&response_type=code&scope=snsapi_userinfo&state=&connect_redirect=1#wechat_redirect

    第二部链接点击后会自动传递code值,跳转到这个方法中去

    public function action_thirdlogin()
    {

      $code = $_GET['code'];
      $appid = $GLOBALS['cfg_weixi_appkey'];
      $appsecret = $GLOBALS['cfg_weixi_appsecret'];
      if ($code) {
        $rs = Common::wechat_login($appid, $appsecret, $code);
      }

      if ($rs['openid']) {}

    }

    第三部执行这个方法获取微信用户的个人信息

    /**
    *
    * 微信授权
    */
    public static function wechat_login($appid, $appsecret, $code)
    {
    $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';

    $token = json_decode(file_get_contents($token_url));
    //var_dump($token);die;
    if (isset($token->errcode)) {
    return false;
    }
    $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $appid . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token;
    //转成对象
    $access_token = json_decode(file_get_contents($access_token_url));
    if (isset($access_token->errcode)) {
    return false;
    }
    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token->access_token . '&openid=' . $access_token->openid . '&lang=zh_CN';
    //转成对象
    $user_info = json_decode(file_get_contents($user_info_url));
    if (isset($user_info->errcode)) {
    return false;
    }
    //var_dump($user_info);die;
    $result = json_decode(json_encode($user_info), true);//返回的json数组转换成array数组
    //var_dump($result);die;
    return $result;//这里就会获取到微信登录用户的个人信息都包含在里面
    }

  • 相关阅读:
    centos下vsftpd不能显示文件,不能创建文件及文件夹
    PHP过滤常用标签的正则表达式
    px、dp、sp、mm、in、pt这些单位有什么区别?
    Android Studio升级后报 method not found: 'runProguard'的错误
    Android应用签名
    Android技巧小结之新旧版本Notification
    java中 synchronized 的使用,确保异步执行某一段代码。
    android开发笔记(二)导入项目到eclipse和另一个项目
    android开发笔记(一)Android studio 输入法
    这个算asp.net的一个bug吗?
  • 原文地址:https://www.cnblogs.com/yszr/p/9330408.html
Copyright © 2011-2022 走看看