18720358503 在线客服 人才招聘 返回顶部
企业动态 技术分享 行业动态

手机微信受权登陆

2021-04-12分享 "> 对不起,没有下一图集了!">

手机微信的受权登陆和QQ、新浪网等服务平台的受权登陆都大同市小异,均选用OauthOAuth2.0鉴权方法。
手机微信受权分成二种:

弹出窗口受权,必须客户手动式愿意


二种scope的差别表明
以snsapi_base为scope进行的网页页面受权,是用于获得进到网页页面的客户的openid的,而且是默然受权并全自动自动跳转到回调函数页的。客户认知的便是立即进到了回调函数页(通常是业务流程网页页面) 为scope进行的网页页面受权,是用于获得客户的基本资料的。但这类受权必须客户手动式愿意,而且因为客户愿意过,因此不必关心,便可在受权后获得该客户的基本资料。

客户管理方法类插口中的“获得客户基本资料插口”,是再用户和微信公众号造成信息互动或关心丧事件消息推送后,才可以依据客户OpenID来获得客户基本资料。这一插口,包含别的手机微信插口,全是必须该客户(即openid)关心了微信公众号后,才可以启用取得成功的。

实际来讲,网页页面受权步骤分成四步:
正确引导客户进到受权网页页面愿意受权,获得code ess_token不一样) ess_token,防止到期 ess_token和openid获得客户基本资料(适用UnionID体制)

下列是封裝的手机微信实际操作类,ess_token、ticket,因为她们具备一定合理期,且每日恳求数有限制,因此开发设计者需自主储存


[PHP] 纯文字查询 拷贝编码
        '0' = '恳求取得成功',         '40001' = 'AppSecret不正确或是AppSecret不归属于这一微信公众号,请开发设计者确定AppSecret的恰当性',         '40002' = '请保证grant_type字段名数值client_credential',         '40164' = '启用插口的IP详细地址没有授权管理中,请在插口IP授权管理中开展设定。',     );     function __construct($appid, $appserect) {         $this- appid = $appid;         $this- appserect = $appserect;         $this- curl = new Curl();     }     /*     手机微信网页页面受权登陆  必须在微信公众号设定 - 作用设定 - 网页页面受权网站域名     第一步:客户愿意受权,获得code     scope : snsapi_base 只有获得openid 立即自动跳转          */     public function getCode($redirect_uri, $scope = '',$state = '1') {         $url = "connect/oauth2/authorize?appid={$this- appid} redirect_uri={$redirect_uri} response_type=code scope={$scope} state={$state}#wechat_redirect";         header("Location:{$url}");         exit;     }     /*     ess_token     */     public essTokenByCode($code) {         $url = "sns/oauth2/access_token?appid={$this- appid} secret={$this- appserect} code={$code} grant_type=authorization_code";         // exit($url);         // $curl = new Curl();         $result = $this- curl- doGet($url);         if (!$result) {             // $this- curl- getError()             $this- msg = "获得token不成功";             return false;         }         $result = json_decode($result, true);         if ($result['errcode']) {             $this- msg = $result['errmsg'];             return false;         }         return $result;     }     // ess_token(假如必须) 根据code 获得openid $type 0默然受权 1弹出窗口受权     public function getUserInfo($code, $type = 0, $lang = 'zh_CN ') {         $result = $this- essTokenByCode($code);             if (!$result) {             return false;         }         $member = C::t(PT_USER)- getByOpenid($result['openid']);     if ($member) {         return $member;     } else {         if ($type) {             $url = "?access_token={$result['access_token']} openid={$result['openid']} lang={$lang}";             // $return = $this- curl- doGet($url);             // 这插口得病 强制性显示信息文档头             $return = file_get_contents($url);             if (!$return) {                 $this- msg = '获得客户信息内容不成功';                 return false;             }             $return = json_decode($return, true);             if (!$return) {                 $this- msg = '获得客户信息内容回到不成功';                 return false;             }             // file_put_contents('ccc.txt',print_r($return,true),FILE_APPEND);             $data = array(                 'openid' = $return['openid'],                 'name' = $return['nickname'],                 'sex' = $return['sex'],                 'province' = $return['province'],                 'city' = $return['city'],                 'country' = $return['country'],                 'img' = $return['headimgurl'],                 'bindtel' = 0,             );         } else {             $data = array(                 'openid' = $result['openid'],                 'username' = "手机微信客户_" . random(6,1)             );         }         $name = rand(100000, );         $e = $name . "";         $password = $e;         $id = UserAddEdit(0, $data['username'], $password, $e,10,0,"", $msg);         if ($id = 0) {             $this- msg = $msg;             return false;         }         C::t(PT_USER)- update($data, $id);         $member = C::t(PT_USER)- get($id);         return $member;         }     }     /*     微信公众号 安全性管理中心 设定IP授权管理     ess_token。开发设计者必须开展妥当储存。access_token的储存最少要保存51两个标识符室内空间。access_token的合理期现阶段为两个钟头,ess_token无效。     */     public essToken($type) {         $addtime = TIMESTAMP - 7200;         $url = "cgi-bin/token?grant_type=client_credential appid={$this- appid} secret={$this- appserect}";         $row = C::t(PT_WXTOKEN)- getNew($addtime, $type);         if ($row) {             return $row['access_token'];         } else {             $result = $this- curl- doGet($url);             if (!$result) {                 $this- msg = "没法获得令牌內容";                 return false;             }             $result = json_decode($result, true);             if (!$result) {                 $this- msg = "分析令牌內容不成功";                 return false;             }             if ($result['access_token']) {                 C::t(PT_WXTOKEN)- addToken($result['access_token'], $type);                 return $result['access_token'];             } else {                 $this- msg = "获得令牌不成功";                 return false;             }         }     }     // 获得js单据  必须在微信公众号设定 - 作用设定 - JS插口安全性网站域名设定     public function getJsTicket() {         $addtime = TIMESTAMP - 7200;         $row = C::t(PT_WXTICKET)- getNew($addtime);         if ($row) {             return $row['ticket'];         } else {             $token = $this- essToken();             if (!$token) {                 return false;             }             $url = "cgi-bin/ticket/getticket?access_token={$token} type=jsapi";             $result = $this- curl- doGet($url);             if (!$result) {                 $this- msg = "没法获得js单据";                 return false;             }             $result = json_decode($result, true);             if (!$result) {                 $this- msg = "分析js单据內容不成功";                 return false;             }             if ($result['ticket']) {                 C::t(PT_WXTICKET)- addTicket($result['ticket']);                 return $result['ticket'];             } else {                 $this- msg = "获得js单据不成功";                 return false;             }         }     }     // js sdk 单据签字 当今网页页面的URL,不包括#以及后边一部分     public function jsSign($data) {         // 1.全部待签字主要参数依照字段名名的ASCII 码自小到大排列(字典序)         ksort($data);         // 2.URL键值对的文件格式(即key1=value1 key2=value2…)拼凑成标识符串string1 选用初始值,不开展URL 转义         $string1 = $this- ToUrlParams($data);         // echo "string1:{$string1} br/         // 3.对string1做sha1数据加密         $sign = sha1($string1);         // echo "signature:{$sign} br/         return $sign;     }     // 获得信息內容     public function getMsg() {         return $this-     }     /**     * 文件格式化主要参数文件格式化成url主要参数     */     public function ToUrlParams($data) {         $buff = "";         foreach ($data as $k = $v) {             if ($k != "sign" $v != "" !is_array($v)) {                 $buff .= $k . "=" . $v . " ";             }         }         $buff = trim($buff, " ");         return $buff;     }
            if (!function_exists("setloginstatus")) {                 include_once libfile('function/member');             }             // 设定登陆情况$wx             setloginstatus($member, 2592000);             checkfollowfeed();             $_G['uid'] = $member['uid'];             $_G['member'] = $member;         } else {             //恳求受权 对主要参数编号             $redirect = urlencode(getProtocol() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);             $wx- getCode($redirect, 'snsapi_base');         }     } function getProtocol() {     return is_HTTPS() ? '/'; function is_HTTPS() {  if ($_SERVER['HTTPS'] === 1 || $_SERVER['HTTPS'] === 'on' || $_SERVER['SERVER_PORT'] == 443) {         return true;     }     return false;

"> 对不起,没有下一图集了!">
在线咨询