微信开发之网页授权 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,去请求接口,就额可以获取用户的详细信息了

三、示例代码

wechat.php 此类是微信基础的操作类

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

/**
 * 微信类
 */
class Wechat extends Controller
{

    protected  $APPID = 'wx9daa4e0c5c26375d';
    protected  $APPSECRET = 'ce3950067aacfb39c997d5def023be98';

    /**
    * 微信服务器配置时 验证token的url
    */
    public function checkToken()
    {
        header("Content-type: text/html; charset=utf-8");

        //1.将timestamp,nonce,toke按字典顺序排序
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $token = 'asd123456zxc';
        $signature = $_GET['signature'];
        $array = array($timestamp,$nonce,$token);
        //2.将排序后的三个参数拼接之后用sha1加密
        $tmpstr = implode('',$array);
        $tmpstr = sha1($tmpstr);
        //3.将加密后的字符串与signature进行对比,判断该请求是否来自微信
        if($tmpstr == $signature){
            echo $_GET['echostr'];
            exit;
        }
    }

    /**
    * 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);
        }
      }
    }

    /**
     * 获取 AccessToken
     */
    public function getAccessToken()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET;

        // 先判断 access_token 文件里的token是否过期,没过期继续使用,过期就更新
        $data = json_decode($this->get_php_file(ROOT_PATH."public".DS."wxtxt".DS."access_token.txt"));
        // 过期 更新
        if ($data->expire_time < time()) {
            
            $res = $this->http_curl($url);
            $access_token = $res['access_token'];
            if ($access_token) {
                // 在当前时间戳的基础上加7000s (两小时)
                $data->expire_time = time() + 7000;
                $data->access_token = $res['access_token'];
                $this->set_php_file(ROOT_PATH."public".DS."wxtxt".DS."access_token.txt",json_encode($data));
            }
        }else{
            // 未过期 直接使用
            $access_token = $data->access_token;
        }
        
        return $access_token;
    }
    
      /**
     * 获取 JsApiTicket
     */
      public function getJsApiTicket()
      {
          // 先判断 jsapi_ticket是否过期 没过期继续使用,过期就更新
          $data = json_decode($this->get_php_file(ROOT_PATH."public".DS."wxtxt".DS."jsapi_ticket.txt"));

          if ($data->expire_time < time()) {
              // 过期 更新
              $accessToken = $this->getAccessToken();
              $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
              $res = $this->http_curl($url);
              $ticket = $res['ticket'];
              if ($ticket) {
                  $data->expire_time = time() + 7000;
                  $data->jsapi_ticket = $ticket;
                  $this->set_php_file(ROOT_PATH."public".DS."wxtxt".DS."jsapi_ticket.txt",json_encode($data));
              }
          }else{
              $ticket = $data->jsapi_ticket;
          }
          return $ticket;
      }


    // 获取存储文件中的token ticket
    private function get_php_file($filename) {
        return trim(file_get_contents($filename));
      }
      // 把token ticket 存储到文件中
      private function set_php_file($filename, $content) {
        $fp = fopen($filename, "w");
        fwrite($fp,  $content);
        fclose($fp);
      }
      
}

wxopera.php 此类是授权类

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

/**
 * 微信功能开发
 */
class Wxopera extends Wechat
{
    /**
     * 网页授权
     */
     public function shouquan(){
         
         $wx = new Wechat();
         $APPID = $wx->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(){
         
         $wx = new Wechat();
         $APPID = $wx->APPID;
         $APPSECRET = $wx->APPSECRET;
         
         // 一、获取用户授权回调里的code  code只有五分钟有效
        echo "<pre>";
         // 获取当前url的参数部分
         $params = $_SERVER["QUERY_STRING"];    // s=/index/Wxopera/callback&code=071W7rvB0IcmQk2z3VuB0ZvNvB0W7rv6&state=STATE
         // 拆分成数组
        $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 = $wx->http_curl($url);
     
         // 三、获取用户信息
         $url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$res['access_token']."&openid=".$res['openid']."&lang=zh_CN";
         
         $userinfo = $wx->http_curl($url2);
         
         print_r($userinfo);
     }
    

}

猜你喜欢

转载自www.cnblogs.com/zxf100/p/12720983.html