码迷,mamicode.com
首页 > 微信 > 详细

微信授权登录

时间:2019-12-18 14:33:58      阅读:978      评论:0      收藏:0      [点我收藏+]

标签:参数   json   struct   size   alac   open   use   avatar   开发者   

一、开始之前,请一定仔细阅读微信开发者文档文档中,总共写了几个步骤:

1、通过appId和需要跳转的路由去请求授权

2、授权之后跳转路由中返回的code 注:前端只需要知道这两个步骤--

建议: 前两步前段实现--后台不要参与; 如果非要后台参与 建议:

A:后台通过接口返回前段拼接好的地址,但是还是前段向微信发请求,

B:前段设置回调地址--获取微信返回的code值然后传给后台

 C:回调地址可以自己添加 参数进行传--个数不限制 --注意点; urlencode($url) 对回调地址进行这样处理一下 

 3、根据code获取access_token

4、根据access_token获取用户信息(snsapi_userinfo授权)

二、前端发起授权请求。这一步需要前端拼凑路由,并且将页面跳转到拼凑路由,

路由规则如:https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的公众appId号&redirect_uri=你的回调路由&response_type=code&scope=你选择的方式&state=STATE#wechat_redirect

注 授权方式可选择为snsapi_userinfo或者snsapi_base,差别请看文档

技术图片

 

 

 

三、点击同意之后,会根据你之前拼凑的回调路由返回code,如下: http://test.***.com/index?code=021Azdiu12zdXd05kkju1ZYkiu1AzdiR&state=1

四、将路由中的code直接传递给后端,让后端做获取用户信息的系列的逻辑处理。

  原生代码:


 1   function getUser(Request $request){
 2 
 3         $code = $_GET["code"];
 4 
 5 //第一步:取全局access_token
 6         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
 7         $token =$this-> getJson($url);
 8 //第二步:取得openid
 9         $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
10         $oauth2 = $this-> getJson($oauth2Url);
11 
12 //第三步:根据全局access_token和openid查询用户信息
13         $access_token = $token["access_token"];
14         $openid = $oauth2[‘openid‘];
15         $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
16 
17         $WeUser = $this-> getJson($get_user_info_url);
18         
19     }

 1  function getJson($url){
 2         $ch = curl_init();
 3         curl_setopt($ch, CURLOPT_URL, $url);
 4         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 5         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 6         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 7         $output = curl_exec($ch);
 8         curl_close($ch);
 9         return json_decode($output, true);
10     }

  用EasyWeChat代码: 

    public function __construct(){
        $this->config =[
            ‘app_id‘ =>‘‘,
            ‘secret‘ =>‘‘,
            ‘response_type‘ => ‘array‘,
        ];
        $this->scopes = [‘snsapi_userinfo‘];//snsapi_base
        $this->app = Factory::officialAccount($this->config);

    }
funcion getUser(Request $request){
   $WeUser= $this->app->oauth->user();
    $watchid = $WeUser->getId(); //微信授权openid //
$nickname= $WeUser->getNickname(); // 对应微信的 nickname $nickname= $this->userTextEncode($WeUser->getNickname()); // 对应微信的 nickname $realname= $WeUser->getName(); // 对应微信的 nickname $avatar= $WeUser->getAvatar(); // 头像网址
}
//昵称中可能会有图片等--进行过滤
1
public function userTextEncode($str){ 2 if(!is_string($str))return $str; 3 if(!$str || $str==‘undefined‘)return ‘‘; 4 5 $text = json_encode($str); //暴露出unicode 6 $text = preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i",function($str){ 7 return addslashes($str[0]); 8 },$text); //将emoji的unicode留下,其他不动,这里的正则比原答案增加了d,因为我发现我很多emoji实际上是\ud开头的,反而暂时没发现有\ue开头。 9 return json_decode($text); 10 }

====================================================================================================================

如果前后台不进行分离-那就简单多了

代码:

   public function  login(){
        $appid=‘wxe8dbaf4ab86f0441‘; //测试账户的appid
        $redirect_uri = urlencode ( ‘http://www.nlyzk.fun/read/login/getUserInfo‘ );
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        header("Location:".$url);

//  用esayWeChat
//        $response = $app->oauth->scopes([‘snsapi_userinfo‘])
//           ->redirect(‘http://www.fgehua.com/h5?pid=555&uid=666‘);
//        $response->send();
//       return $response;
    }

  

 

 

 

  

 

 

技术图片

微信授权登录

标签:参数   json   struct   size   alac   open   use   avatar   开发者   

原文地址:https://www.cnblogs.com/liyanxi/p/12059563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!