本文主要是介绍thinkPHP3.2 微信登录(微信扫码登录),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
define("APPID", "fff");//你微信定义的appiddefine("APPSECRET","ffff");//你微信公众号的appsecret
//微信登录
//第一步:用户同意授权,获取code
public function acceptAction(){
//这个链接是获取code的链接 链接会带上code参数
$REDIRECT_URI = "http://你的域名/Home/Index/getToken";
echo $REDIRECT_URI."<br>";
$REDIRECT_URI = urlencode($REDIRECT_URI);
echo $REDIRECT_URI."<br>";
$scope = "snsapi_login";
echo $scope."<br>";
$state = md5(mktime());
echo $state."<br>";
//https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect
//网站将微信登录二维码内嵌到自己页面中,用户使用微信扫码授权后通过js将code返回给网站。
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=".APPID."&redirect_uri=".$REDIRECT_URI."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
//如果是微信客户端登录的话,把url换成($url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$REDIRECT_URI."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";)这个就可以了
header("location:$url");
}
//用户同意之后就获取code 通过获取code可以获取一切东西了
public function getTokenAction(){
//获取accse_token
$code = $_GET["code"];
//echo $code;
//echo "<br>";
//用code获取access_yoken
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
//这里可以获取全部的东西 access_token openid scope
$res = $this->https_requestAction($url);
$res = json_decode($res,true);
$openid = $res["openid"];
//echo "<pre>";
//print_r($res);
//echo $openid;
//echo "<br>";
$access_token = $res["access_token"];
//echo $access_token;
//这里是获取用户信息
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->https_requestAction($url);
$res = json_decode($res,true);
//写入session
//print_r($res);
//把用户的信息写入session 以备查用
//查询数据库中是否存在该微信用户
$user=M('users');
$nums=$user->where(array("wxopenid"=>$openid))->count();
$_SESSION['wxopenid']=$res['openid'];
$_SESSION['wxnickname']=$res['nickname'];
$_SESSION['wxhead']=$res['headimgurl'];
//echo $nums;
if($nums>0){
$result=$user->where(array('wxopenid'=>$openid))->find();
$_SESSION = $result;
$_SESSION['logtype']='wxcontect';//标记用户登录的类型为QQ登录
$_SESSION['bind']='yes';//标记用户是否绑定了手机号
//判断手否手机端访问
if($this->isMobileAction()){
$this->redirect('Web/User/index');
exit();
}
$this->redirect("/Home/User/userinfo");
}
//直接跳到首页
else{
$_SESSION['logtype']='wxcontect';
$_SESSION['userid']=2;
$_SESSION['bind']='no';
$this->redirect("/Home/Index/index");
}
//$weixn = $res["openid"];
//$nickname = $res["nickname"];
//$_SESSION["weixin"]=$weixin;
//header("location:http://你的域名/Home/Index/index");
}
public function https_requestAction($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
这篇关于thinkPHP3.2 微信登录(微信扫码登录)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!