微信开发之网页授权 PHP

微信开发文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

一、公众号配置

在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;

 授权登陆有两种:一种是 snsapi_base 静默登陆,登陆的时候不会弹出授权弹窗,但是这种方式只能获取 openid;另一种是 snsapi_userinfo,这种方式会弹出授权框,需要用户手动同意,可以获取到用户的详细信息
access_token:网页授权用的access_token和基础支持中的“获取access_token”的接口获取的普通access_token不同;

二、授权流程

 1、用户同意授权,获取code
在需要授权的地方引导用户进入授权页面

 用户同意授权后,页面会跳转到之前填写的回调页面,在回调地址url后面会携带code参数,截取url就可以得到code了
注意:每次授权携带的code都不一样,也有过期时间

 2、通过code换取网页授权access_token
如果之前选择的授权作用域是snsapi_base,那么在本步骤获取access_token的同时,openid也会获取到,那么snsapi_base方式的网页授权也就到此结束了;如果用的是snsapi_userinfo,那还要继续往下。
需要注意的是access_token有过期时间,所以注意这个时间,避免access_token过期。

3、获取用户信息

通过前几步获取到的access_token和openid,去请求接口,就额可以获取用户的详细信息了

三、示例代码

<?php
namespace app\index\controller;
use think\Controller;


/**
 * 微信功能开发
 */
class Wxopera extends Wechat
{
    
    protected  $APPID = 'xxxxxxxxxxxxxxx';
    protected  $APPSECRET = 'xxxxxxxxxxxxxxx';

    /**
    * curl请求 
    */
    public function http_curl($url, $type = 'get', $res = 'json', $arr = ''){
        
      $cl = curl_init();
      curl_setopt($cl, CURLOPT_URL, $url);
      curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, false);
      if($type == 'post'){
        curl_setopt($cl, CURLOPT_POST, 1);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $arr);
      }
      $output = curl_exec($cl);
      curl_close($cl);
      return json_decode($output, true);
      if($res == 'json'){
        if( curl_error($cl)){
          return curl_error($cl);
        }else{
          return json_decode($output, true);
        }
      }
    }

    /**
     * 网页授权
     */
     public function shouquan(){
         
         $APPID = $this->APPID;
         // 编码
         $redirect = urlencode("http://test.zizhuyou.site/index/Wxopera/callback");
        
        // 调起微信授权提示
         $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        // 跳转授权页面
        $this->redirect($url);
     
     }
     
     /**
     * 网页授权的回调
     */
     public function callback(){
         
         $APPID = $this->APPID;
         $APPSECRET = $this->APPSECRET;
         
         // 一、获取用户授权回调里的code  code只有五分钟有效
        echo "<pre>";
         // 获取当前url的参数部分
         $params = $_SERVER["QUERY_STRING"];    // s=/index/Wxopera/callback&code=code&state=STATE
         // 拆分成数组 得到code
        $arr = explode('&',$params);
        $code = explode('=',$arr[1]);
        $code = $code[1];   
        
        // 二、通过code获取网页授权access_token 有效期7200s,过期需要重新获取,这里暂不处理过期的问题
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$APPID&secret=$APPSECRET&code=$code&grant_type=authorization_code";
         $res = $this->http_curl($url);
     
         // 三、获取用户信息
         $url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$res['access_token']."&openid=".$res['openid']."&lang=zh_CN";
         
         $userinfo = $this->http_curl($url2);
         
         print_r($userinfo);
     }
    

}

思路说明:

1.公众号配置:填写回调地址,域名即可

2.调起微信授权提示页面(snsapi_base 静默登陆,snsapi_userinfo 手动授权)

3.在回调函数里接收返回的参数 code(有效期五分钟)

4.拿 code 换取 openid 和 token(默认7200s),此时已经可以获取到 openid,但还获取不到详细信息

5.获取用户详细信息,拿 token 和 openid 获取用户详细信息

 

posted @ 2020-04-17 16:50  下页、再停留  阅读(1320)  评论(0编辑  收藏  举报