zoukankan      html  css  js  c++  java
  • 网页授权

    /**
    * 网页授权获取用户openid
    * @return [type] [description]
    */
    public static function getOpenid()
    {
      //echo 1;die;
      //先去session里取openid
      $openid = session('openid');
      //var_dump($openid);die;
      if(!empty($openid)){
        return $openid;
      }
    //微信授权成功后 跳转咱们配置的地址 (回调地址)带一个code参数
      $code = request()->input('code');
    if(empty($code)){
      //没有授权 跳转到微信服务器进行授权
      $host = $_SERVER['HTTP_HOST']; //域名
      $uri = $_SERVER['REQUEST_URI']; //路由参数
      $redirect_uri = urlencode("http://".$host.$uri); // ?code=xx
      $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".self::appid."&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
      header("location:".$url);die;
    }else{
      //通过code换取网页授权access_token
      $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".self::appid."&secret=".self::secret."&code={$code}&grant_type=authorization_code";
      $data = file_get_contents($url);
      $data = json_decode($data,true);
      $openid = $data['openid'];
      //获取到openid之后 存储到session当中
      session(['openid'=>$openid]);
      return $openid;
      //如果是非静默授权 再通过openid access_token获取用户信息
      }
    }

    //简单示例仅供参考

  • 相关阅读:
    springMVC整合mybatis框架的项目起始配置
    springMVC解决JSON对象乱码问题
    SpringBoot将redis和spring-cache集成使用
    idea实现右侧预览
    List循环遍历时移出元素
    Mac下IDEA卡顿解决方案
    swagger中@ApiModelProperty中example属性对List的支持
    centos7安装PostgreSQL12
    mongo添加索引及索引相关方法
    Mac下安装Postgresql
  • 原文地址:https://www.cnblogs.com/dsds/p/11634110.html
Copyright © 2011-2022 走看看