使用thinkphp3.2.3开发微信授权登录详细教程

 5163

最近开发微信,涉及到微信用户登录的问题,研究了一下还是搞定了,整理了一下跟大家贴出来我写的这个代码吧,需要用的东西我都已经定义成常量,需要的话直接修改,以下是代码:

  1. <?php
  2. namespace Apps\Controller;
  3. use Common\Controller\AppBaseController;
  4. define("TOKEN", "");//你微信定义的token
  5. define("APPID", "");//你微信定义的appid
  6. define("APPSECRET","");//你微信公众号的appsecret
  7. class TestController extends AppBaseController //继承的这个控制器没用 你可以继承其他的任意一个
  8. {
  9.     //第一步:用户同意授权,获取code
  10.     function accept(){
  11.         //这个链接是获取code的链接 链接会带上code参数
  12.         $REDIRECT_URI = "http://www.yourdomain.com/wxt_webhome/index.php/Apps/Test/getCode";
  13.         echo $REDIRECT_URI."<br>";
  14.         $REDIRECT_URI = urlencode($REDIRECT_URI);
  15.         echo $REDIRECT_URI."<br>";
  16.         $scope = "snsapi_userinfo";
  17.         echo $scope."<br>";
  18.         $state = md5(mktime());
  19.         echo $state."<br>";
  20.         $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$REDIRECT_URI."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
  21.         header("location:$url");
  22.         
  23.     }
  24.     //用户同意之后就获取code  通过获取code可以获取一切东西了  机智如我
  25.     function getCode(){
  26.         //获取accse_token
  27.         $code = I("code");
  28.         //echo $code;
  29.         //echo "<br>";
  30.         //用code获取access_yoken
  31.         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
  32.         //这里可以获取全部的东西  access_token openid scope
  33.         $res = $this->https_request($url);
  34.         $res  = json_decode($res,true);
  35.         $openid = $res["openid"];
  36.         echo "<pre>";
  37.         //print_r($res);
  38.         //echo $openid;
  39.         //echo "<br>";
  40.         $access_token = $res["access_token"];
  41.         //echo $access_token;
  42.         //这里是获取用户信息
  43.         $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
  44.         $res = $this->https_request($url);
  45.         $res = json_decode($res,true);
  46.         //写入session
  47.         print_r($res);
  48.         //把用户的信息写入session 以备查用
  49.         $weixn = $res["openid"];
  50.         $nickname = $res["nickname"];
  51.         session('weixin',$weixin);
  52.         header("location:http://www.yourdomain.com/wxt_webhome/test.php");
  53.     }
  54.     function https_request($url, $data = null)
  55. {
  56.     $curl = curl_init();
  57.     curl_setopt($curl, CURLOPT_URL, $url);
  58.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  59.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  60.     if (!empty($data)){
  61.         curl_setopt($curl, CURLOPT_POST, 1);
  62.         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  63.     }
  64.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  65.     $output = curl_exec($curl);
  66.     curl_close($curl);
  67.     return $output;
  68. }
  69. }//classend


本文网址:https://www.zztuku.com/detail-7664.html
站长图库 - 使用thinkphp3.2.3开发微信授权登录详细教程
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    最新PTCMS 14个采集规则