PHP 第三方QQ登录[PC、手机共用]

6 篇文章 0 订阅
1 篇文章 0 订阅

先去https://open.tencent.com/ 里面的QQ互联申请,成为开发者后添加网站应用进行审核。
应用必须在需要用到的网站先添加QQ登录图标,不然会审核失败

前端代码:

 <a  onclick='toQzoneLogin()' openwindowheight="350" openwindowwidth="450" data-third="qq" class="qq icon" pbtag="qq" pbflag="头部" title="QQ登录"></a>
 <script type="text/javascript">
 var childWindow;
 function toQzoneLogin()
 {
     childWindow = window.location.href = "接收回调的地址"; //这个地址必须在QQ互联里面对应的应用配置好,可多个
     //childWindow = window.open("接收回调的地址","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1"); 新窗口打开
 }  
</script>

后端代码:

   public function qq_login()
    { 
          
        header("Content-Type: text/html;charset=utf-8"); 

        $qqConfig = C('THINK_SDK_QQ'); 
        $appid     = $qqConfig['APPID']; 
        $appkey = $qqConfig['APPKEY'];
        $url = $qqConfig['CALLBACK'];
      
        //1:获取code
        session_start();
        $code = $_REQUEST["code"]; 
        if(empty($code)) { 
            $_SESSION['state'] = md5(uniqid(rand(), TRUE)); 
            $ret_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$appid."&redirect_uri=".urlencode($url)."&state=".$_SESSION['state'];
            echo("<script> top.location.href='".$ret_url."'</script>");
        } 
        //2:通过code获取accesstoken
        if($_REQUEST['state'] == $_SESSION['state'] || 1) { 
            $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=".$appid."&redirect_uri=".urlencode($url)."&client_secret=".$appkey."&code=".$code;
            $response = file_get_contents($token_url);
        
            //如果用户临时改变主意取消登录,返回true!==false,否则执行step3  
            if (strpos($response, "callback") !== false) {
                $lpos = strpos($response, "(");
                $rpos = strrpos($response, ")");
                $response = substr($response, $lpos + 1, $rpos - $lpos -1);
                $msg = json_decode($response);
                if (isset($msg->error)) {
                    echo "<h3>error:</h3>".$msg->error;
                    echo "<h3>msg :</h3>".$msg->error_description;
                    exit;
                }
            }
      
            //3:使用accesstoken来获取用户openid
            $params = array();
            parse_str($response, $params);//数据参数变量化
            $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
            $str = file_get_contents($graph_url);
            if (strpos($str, "callback") !== false) {
                $lpos = strpos($str, "(");
                $rpos = strrpos($str, ")");
                $str = substr($str, $lpos + 1, $rpos - $lpos -1);
            }
            $user = json_decode($str,true);//存放返回的数据 client_id ,openid
            if (isset($user->error)) {
                echo "<h3>error:</h3>".$user['error'];
                echo "<h3>msg :</h3>".$user['error_description'];
                exit;
            }
      
            //4:使用openid和accesstoken获取用户信息
            $user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user['openid']}&format=json"; 
            $userInfo = file_get_contents($user_data_url);//获取到的用户信息
            
            $userInfo = json_decode($userInfo,true); 
            
            if($userInfo){  
                //以下为授权成功后的自定义操作 
            }else{
                echo '未知错误';
            }
        }else{
            echo("The state does not match. You may be a victim of CSRF.");
        } 
    }

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值