admin管理员组

文章数量:1559095

调用qq登录接口,首先要到qq互联完善开发者认证信息,并通过审核,然后创建一个网站应用,获得app id和app key,通过审核后即可调用基本接口get_user_info(获得用户信息),实现qq登录网站功能。

废话不多,上示例代码(qq登录李维山博客):

 top.location.href='".$dialog_url."'");
    }
  
    //step2:通过authorization code获取access token
    if($_request['state'] == $_session['state'] || 1) {
        //拼接url
        $token_url = "https://graph.qq/oauth2.0/token?grant_type=authorization_code&"."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&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 "

error:

".$msg->error; echo "

msg :

".$msg->error_description; exit; } } //step3:使用access token来获取用户的openid $params = array(); parse_str($response, $params);//把传回来的数据参数变量化 $graph_url = "https://graph.qq/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);//存放返回的数据 client_id ,openid if (isset($user->error)) { echo "

error:

".$user->error; echo "

msg :

".$user->error_description; exit; } //step4:使用openid和access_token获取用户信息 $user_data_url = "https://graph.qq/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user->openid}&format=json"; $user_data = file_get_contents($user_data_url);//获取到的用户信息 //以下为授权成功后的自定义操作 if($user_data){ // ...... echo(""); }else{ echo '未知错误'; } }else{ echo("the state does not match. you may be a victim of csrf."); }

登录效果:

本文标签: qq