douxian1939 2014-08-23 17:06
浏览 505
已采纳

我如何获得Facebook用户名?

Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:

<?php 
    require '../src/facebook.php';

    $facebook = new Facebook(array(
      'appId'  => 'XXXXXXXXXXXXXXXX',
      'secret' => 'XXXXXXXXXXXXXXXXXx',
      'cookie' => true
    ));
?>

<?php
    $loginUrl = $facebook->getLoginUrl(array ( 
            'scope'     => 'email',
            'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
            ));

    $user = $facebook->getUser();
    echo '<a href = "'.$loginUrl.'">Login Here </a> ';

?>

And this is the part where I want to get some informations about the user:

<?php
    require '../src/facebook.php';
    $facebook = new Facebook(array(
          'appId'  => 'XXXXXXXXXXXXX',
          'secret' => 'XXXXXXXXXXXXX',
          'cookie' => true
        ));
    $user = $facebook->getUser();

    if ($user) {
        try{
            $user_profile = $facebook->api('/me');
            var_dump($user_profile);
            $fbid = $user_profile['id'];                 // To Get Facebook ID
            $fbuname = $user_profile['username'];  // To Get Facebook Username
            $fbfullname = $user_profile['name']; // To Get Facebook full name
            $femail = $user_profile['email'];    // To Get Facebook email ID
        }catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }

        echo "FBID ".$fbid."<br>";
        echo "Email ".$femail."<br>";
        echo "Name ".$fbfullname."<br>";
        echo "Username ".$fbuname."<br>";

        echo "Facebook Picture:<br>";
        //echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
        echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
    }
?>

This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .

  • 写回答

1条回答 默认 最新

  • dtwupu6414 2014-08-23 17:17
    关注

    Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user

    Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.

    You should be able to use the App Scoped ID to show the user picture:

    echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
    

    Btw, i would suggest using the new PHP SDK (4.x) for new Apps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?