使用最新的 API 获取 Facebook 好友列表

发布于 2024-09-26 02:47:25 字数 821 浏览 5 评论 0原文

我正在使用 最新版本的 Facebook SDK (它可以连接到名为“图形 API”虽然我不确定)。我已经修改了 Facebook 的示例代码,让我可以连接到 Facebook,这很有效……但我无法获取我的朋友列表。

$friends = $facebook->api('friends.get');

这会产生错误消息:“致命错误:未捕获的 OAuthException:(#803) 您请求的某些别名不存在:friends.get 在第 543 行的 /mycode/facebook.php 中抛出”

不知道为什么会这样或是什么方法。有人可以告诉我获取好友列表的正确语法(最新的 Facebook API)吗? (我尝试了“$friends = $facebook->api->friends_get();”并得到了不同的错误,“致命错误:在 /mycode/example 中的非对象上调用成员函数friends_get()。 我可以确认,在我的代码中的

这一点之前,一切都很好:我已通过有效会话连接到 Facebook,我可以获取我的信息并将其转储到屏幕上......即这个代码在失败的 Friends.get 调用之前完美执行:

$session = $facebook->getSession();
if ($session) {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
}
print_r($me);

I'm using the most recent version of the Facebook SDK (which lets to connect to something called the 'graph API' though I'm not sure). I've adapted Facebook's example code to let me connect to Facebook and that works... but I can't get a list of my friends.

$friends = $facebook->api('friends.get');

This produces the error message: "Fatal error: Uncaught OAuthException: (#803) Some of the aliases you requested do not exist: friends.get thrown in /mycode/facebook.php on line 543"

No clue why that is or what that means. Can someone tell me the right syntax (for the latest Facebook API) to get a list of friends? (I tried "$friends = $facebook->api->friends_get();" and get a different error, "Fatal error: Call to a member function friends_get() on a non-object in /mycode/example.php on line 129".)

I can confirm that BEFORE this point in my code, things are fine: I'm connected to Facebook with a valid session and I can get my info and dump it to the screen just... i.e. this code executes perfectly before the failed friends.get call:

$session = $facebook->getSession();
if ($session) {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
}
print_r($me);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

长发绾君心 2024-10-03 02:47:25

我想这就是你想要的:

$friends = $facebook->api('/me/friends');

I think this is what you want:

$friends = $facebook->api('/me/friends');
趴在窗边数星星i 2024-10-03 02:47:25

这是 PHP 代码的实时版本,可让您从 Facebook 获取好友

<?php
    $user = $facebook->getUser();


    if ($user) {
        $user_profile = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');

        echo '<ul>';
        foreach ($friends["data"] as $value) {
            echo '<li>';
            echo '<div class="pic">';
            echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
            echo '</div>';
            echo '<div class="picName">'.$value["name"].'</div>'; 
            echo '</li>';
        }
        echo '</ul>';
    }
?>

This is live version of PHP Code to get your friends from Facebook

<?php
    $user = $facebook->getUser();


    if ($user) {
        $user_profile = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');

        echo '<ul>';
        foreach ($friends["data"] as $value) {
            echo '<li>';
            echo '<div class="pic">';
            echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
            echo '</div>';
            echo '<div class="picName">'.$value["name"].'</div>'; 
            echo '</li>';
        }
        echo '</ul>';
    }
?>
从来不烧饼 2024-10-03 02:47:25

结交@nfvs 描述的朋友是一个好方法。它输出一个多维数组,其中包含具有属性 id 和 name 的所有朋友(按 id 排序)。你可以看到这样的朋友照片:

foreach ($friends as $key=>$value) {
    echo count($value) . ' Friends';
    echo '<hr />';
    echo '<ul id="friends">';
    foreach ($value as $fkey=>$fvalue) {
        echo '<li><img src="https://graph.facebook.com/' . $fvalue->id . '/picture" title="' . $fvalue->name . '"/></li>';
    }
    echo '</ul>';
}

Getting the friends like @nfvs describes is a good way. It outputs a multi-dimensional array with all friends with attributes id and name (ordered by id). You can see the friends photos like this:

foreach ($friends as $key=>$value) {
    echo count($value) . ' Friends';
    echo '<hr />';
    echo '<ul id="friends">';
    foreach ($value as $fkey=>$fvalue) {
        echo '<li><img src="https://graph.facebook.com/' . $fvalue->id . '/picture" title="' . $fvalue->name . '"/></li>';
    }
    echo '</ul>';
}
弃爱 2024-10-03 02:47:25

对于任何使用 Graph API v2.0 遇到这个问题的人来说,请注意:这将不再起作用。在 v2.0 中,调用 /me/friends 只会返回也使用该应用程序的朋友的列表:

  • 需要具有 user_friends 权限的用户访问令牌才能查看当前用户的好友。
  • 这只会返回所有使用过(通过 Facebook 登录)该应用发出请求的好友。
  • 如果此人的好友拒绝 user_friends 权限,则该好友将不会出现在此人的好友列表中。

Just a heads up for anyone stumbling across this question using v2.0 of the Graph API: This will not work anymore. In v2.0, calling /me/friends only returns a list the person's friends who also use the app:

  • A user access token with user_friends permission is required to view the current person's friends.
  • This will only return any friends who have used (via Facebook Login) the app making the request.
  • If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.
墨小墨 2024-10-03 02:47:25

如果您想使用 REST 端点,

$friends = $facebook->api(array('method' => 'friends.get'));

或者如果您使用图形 api,则使用,

$friends = $facebook->api('/me/friends');

If you want to use the REST end point,

$friends = $facebook->api(array('method' => 'friends.get'));

else if you are using the graph api, then use,

$friends = $facebook->api('/me/friends');
三生池水覆流年 2024-10-03 02:47:25

在最新版本的 facebook sdk 中,出于安全原因,facebook 禁用了允许您访问某个好友列表的功能...查看文档以了解更多信息...

in the recent version of facebook sdk , facebook has disabled the feature that let you access some one friends list due to security reasons ... check the documentation to learn more ...

呆头 2024-10-03 02:47:25

使用 FQL 代替,它类似于 SQL,但适用于 Facebook 的数据表,并且可以轻松涵盖您想要进行的数据查询。
您不必使用所有这些 /xx/xxx/xx 调用,只需知道您感兴趣的表和列即可。

$myQuery = "SELECT uid2 FROM friend WHERE uid1=me()";
$facebook->api( "/fql?q=" . urlencode($myQuery) )

精彩的交互式示例位于 http://developers.facebook.com/docs/reference/fql/

Use FQL instead, its a like SQL but for Facebook's data tables and easily covers data query you'de like to make.
You won't have to use all of those /xx/xxx/xx calls, just know the tables and columns you are intereseted in.

$myQuery = "SELECT uid2 FROM friend WHERE uid1=me()";
$facebook->api( "/fql?q=" . urlencode($myQuery) )

Great interactive examples at http://developers.facebook.com/docs/reference/fql/

快乐很简单 2024-10-03 02:47:25

使用 CodeIgniter OAuth2/0.4.0 火花,

在 Auth.php 文件中,

$user = $provider->get_user_info($token);

$friends = $provider->get_friends_list($token);
print_r($friends);

并在 Provider 下的 Facebook.php 文件中添加以下函数,

    public function get_friends_list(OAuth2_Token_Access $token)
    {
            $url = 'https://graph.facebook.com/me/friends?'.http_build_query(array(
                    'access_token' => $token->access_token,
            ));
            $friends = json_decode(file_get_contents($url),TRUE);

            return $friends;
    }

prints the facebenter code hereook friends.

Using CodeIgniter OAuth2/0.4.0 sparks,

in Auth.php file,

$user = $provider->get_user_info($token);

$friends = $provider->get_friends_list($token);
print_r($friends);

and in Facebook.php file under Provider, add the following function,

    public function get_friends_list(OAuth2_Token_Access $token)
    {
            $url = 'https://graph.facebook.com/me/friends?'.http_build_query(array(
                    'access_token' => $token->access_token,
            ));
            $friends = json_decode(file_get_contents($url),TRUE);

            return $friends;
    }

prints the facebenter code hereook friends.
魂ガ小子 2024-10-03 02:47:25
friends.get

是获取朋友列表的功能

,是的,这是最好的

    $friends = $facebook->api('/me/friends');

    echo '<ul>';
    foreach ($friends["data"] as $value) {
        echo '<li>';
        echo '<div class="pic">';
        echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
        echo '</div>';
        echo '<div class="picName">'.$value["name"].'</div>'; 
        echo '</li>';
    }
    echo '</ul>';
friends.get

Is function to get a list of friend's

And yes this is best

    $friends = $facebook->api('/me/friends');

    echo '<ul>';
    foreach ($friends["data"] as $value) {
        echo '<li>';
        echo '<div class="pic">';
        echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
        echo '</div>';
        echo '<div class="picName">'.$value["name"].'</div>'; 
        echo '</li>';
    }
    echo '</ul>';
因为看清所以看轻 2024-10-03 02:47:25

header('内容类型:text/html; charset=utf-8');

在您的页面中输入。

header('Content-type: text/html; charset=utf-8');

input in your page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文