微信扫码登陆

  • 标签: 微信
  • 发表时间:2018年05月02日
  • 作者:hhao
  • 浏览次数:699

一,微信开放平台创建网站应用(https://open.weixin.qq.com/)

获取AppID参数与AppSecret;

二, 实现扫码登陆源码

namespace Admin\\Controller;


use Think\\Controller;

class LoginController extends Controller
{
 //-------配置
 protected  $AppID = '你的AppID';
 protected  $AppSecret = '你的Secret';
 protected $callback  =  '扫码回调地址'; //回调地址
 public function index(){

 //微信登录

            
//-------生成唯一随机串防CSRF攻击
 
 $callback = urlencode($this->callback);
 $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login#wechat_redirect";
 header("Location: $wxurl");
 
 public function getInfo(){
           
 $AppID = $this->AppID;
 $AppSecret = $this->AppSecret;
 $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
 $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
 $json =  curl_exec($ch);
        curl_close($ch);
 $arr=json_decode($json,1);

 // p($arr);
 $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
 $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
 $json =  curl_exec($ch);
        curl_close($ch);
 $arr=json_decode($json,1);
 //得到 用户资料
 var_dump($arr);


    }
 }

本代码并非完美!

评论