标题:qq 互联能获取到qq用户信息,不回单独显示,比如单独显示昵称,单独显示qq头 ...
只看楼主
guang2356447
Rank: 2
等 级:论坛游民
帖 子:434
专家分:31
注 册:2007-7-10
结帖率:65.85%
 问题点数:0 回复次数:1 
qq 互联能获取到qq用户信息,不回单独显示,比如单独显示昵称,单独显示qq头像
程序代码:
<?php
    header("Content-Type: text/html;charset=utf-8");
    //应用APP ID
    $app_id = "101937514";
    //应用APP Key
    $app_secret = "f573c5f4f1e8ad8fafeef2f132c30d19";
    //应用填写的网站回调域
    $my_url = "http://www.qxlm2021.xyz/fabu.php";
  
    //Step1:获取Authorization Code
    session_start();
    $code = $_REQUEST["code"];//存放Authorization Code
    if(empty($code)) {
        //state参数用于防止CSRF攻击,成功授权后回调时原样带回
        $_SESSION['state'] = md5(uniqid(rand(), TRUE));
        //拼接URL
        $dialog_url = "https://graph.$app_id."&redirect_uri=".urlencode($my_url)."&state=".$_SESSION['state'];
        echo("<script> top.location.href='".$dialog_url."'</script>");
    }
  
    //Step2:通过Authorization Code获取Access Token
    if($_REQUEST['state'] == $_SESSION['state'] || 1) {
        //拼接URL
        $token_url = "https://graph.$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 "<h3>error:</h3>".$msg->error;
                echo "<h3>msg :</h3>".$msg->error_description;
                exit;
            }
        }
  
        //Step3:使用Access Token来获取用户的OpenID
        $params = array();
        parse_str($response, $params);//把传回来的数据参数变量化
        $graph_url = "https://graph.$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 "<h3>error:</h3>".$user->error;
            echo "<h3>msg :</h3>".$user->error_description;
            exit;
        }
        //Step4:使用openid和access_token获取用户信息
        $user_data_url = "https://graph.{$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("<script> top.location.href='http://www.qxlm2021.xyz'</script>");
            echo "1";
            echo "<br>";
            echo $user->openid;
            echo "<br>";
            echo "2";
            echo "<br>";
            echo $user_data_url;
            echo "<br>";
            echo "3";
            echo "<br>";
            echo $user_data;//qq所有信息
            echo "<br>";
            echo "4";
            echo "<br>";
            echo $user->nickname;//这个不显示   怎么显示这个qq的昵称  语法怎么写
            echo "<br>";
            echo "<br>";
            
            
        }else{
            echo '未知错误';
        }
    }else{
        echo("The state does not match. You may be a victim of CSRF.");
    }
?>
搜索更多相关主题的帖子: echo qq 获取 显示 Code 
2021-03-11 18:26
gdk2006
Rank: 4
等 级:业余侠客
威 望:8
帖 子:928
专家分:270
注 册:2006-7-2
得分:0 
json可以转数组操作。

程序员的悲哀如何找女朋友?
追女解决方案百度“让她着迷”!
2021-05-07 11:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505089-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.197471 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved