zoukankan      html  css  js  c++  java
  • 微信网页开发

    微信网站一般是先要微信网页授权后获取到access_token,才有资格获取用户信息的,所以如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。

    第一步是先获取用户授权(具体的请看微信开发者文档):

          授权也分两种:静态授权和手动授权:

            关于网页授权的两种scope的区别说明

         1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

         2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。 

         3、用户管理类接口中的“获取用户基本信息接口”,是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。

                     

    1     /**
    2      * 网页授权获取用户openid(静默授权)
    3      */
    4     public function getOpenid($code){
    5         $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='. Wxapi::APPID . '&secret=' . Wxapi::APPSECRET . '&code=' . $code . '&grant_type=authorization_code';
    6         $result = json_decode($this->CurlGet($url),true);  
    7         return $result;
    8     }

    请参考文档来理解:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

     1     /**
     2      * 网页授权获取用户基本信息(手动同意)
     3      */
     4     public function getUserInfo($code,$refresh_token){
     5         if(!empty($code)){
     6             //code换取网页授权access_token
     7             $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='. Wxapi::APPID . '&secret=' . Wxapi::APPSECRET . '&code=' . $code . '&grant_type=authorization_code';
     8             $result = json_decode($this->CurlGet($url),true);  
     9         }else if(!empty($refresh_token)){
    10             //刷新access_token
    11             $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . Wxapi::APPID . '&grant_type=refresh_token&refresh_token=' . $refresh_token;
    12             $result = json_decode($this->CurlGet($url),true);  
    13             if(!empty($result['errcode'])){
    14                 $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . Wxapi::APPID . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";//refresh_token过期失效重新授权
    15                 header("Location:$url");//跳转授权页面
    16             }
    17         }else{
    18             return array('errcode' => '???','errmsg' => '请传入code或refresh_token');
    19         }
    20         
    21         //错误返回处理
    22         if(!empty($result['errcode'])){
    23             return $result;
    24         }else{
    25             //拉取用户信息
    26             $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $result['access_token'] . '&openid=' . $result['openid'] . '&lang=zh_CN';
    27             $user_info = json_decode($this->CurlGet($url),true); 
    28             $user_info['refresh_token'] = $result['refresh_token'];
    29             
    30             return $user_info;
    31         }
    32     }

    相关资料:


    json_decode

    PHP json_decode() 函数用于对 JSON 格式的字符串进行解码,并转换为 PHP 变量。

    语法

    mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

    参数

    • json_string: 待解码的 JSON 字符串,必须是 UTF-8 编码数据

    • assoc: 当该参数为 TRUE 时,将返回数组,FALSE 时返回对象。

    • depth: 整数类型的参数,它指定递归深度

    • options: 二进制掩码,目前只支持 JSON_BIGINT_AS_STRING 。

    CurlGet : 将url发送出去,返回相关数据
  • 相关阅读:
    美的书,献给所有追求美的人
    《WCF揭秘》:欢迎大家来找我的茬(有奖)!
    这个寒冬,如何让我们的身价翻倍?
    微软中文论坛周年Party掠影
    ASP.NET 3.5之屠龙刀——《ASP.NET高级程序设计(第2版)》
    Red Hat Linux指南
    一部孟宪会推荐的C#图解教程
    左菜单js效果
    分享图标
    谷歌hack
  • 原文地址:https://www.cnblogs.com/laijinquan/p/7483617.html
Copyright © 2011-2022 走看看