php 使用最新 API 获取 Facebook 好友列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3835028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting list of Facebook friends with latest API
提问by Eric
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.
我正在使用Facebook SDK 的最新版本(尽管我不确定,它可以连接到称为“图形 API”的东西)。我已经修改了 Facebook 的示例代码,让我可以连接到 Facebook,这很有效……但我无法获得我的朋友列表。
$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"
这会产生错误消息:“致命错误:未捕获的 OAuthException:(#803)您请求的某些别名不存在:friends.get 在第 543 行的 /mycode/facebook.php 中抛出”
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".)
不知道为什么会这样或这意味着什么。有人可以告诉我获取好友列表的正确语法(针对最新的 Facebook API)吗?(我试过“$friends = $facebook->api->friends_get();”并得到一个不同的错误,“致命错误:在/mycode/example.php 上的非对象上调用成员函数friends_get()第 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:
我可以确认在我的代码中的这一点之前,一切都很好:我通过有效的会话连接到 Facebook,我可以获取我的信息并将其转储到屏幕上......即此代码在失败的朋友之前完美执行.get 电话:
$session = $facebook->getSession();
if ($session) {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
}
print_r($me);
回答by nfvs
I think this is what you want:
我认为这就是你想要的:
$friends = $facebook->api('/me/friends');
回答by Yash Kumar
This is live version of PHP Code to get your friends from Facebook
这是 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>';
}
?>
回答by geoffa
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:
像@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>';
}
回答by Mads Foli Bjerre
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:
对于使用 Graph API v2.0 遇到此问题的任何人,请提一下:这将不再起作用。在 v2.0 中,调用 /me/friends 只会返回一个列表,该人的好友也使用该应用程序:
- 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.
- 需要具有 user_friends 权限的用户访问令牌才能查看当前人员的好友。
- 这只会返回任何使用过(通过 Facebook 登录)发出请求的应用程序的朋友。
- 如果此人的好友拒绝了 user_friends 权限,则该好友将不会出现在此人的好友列表中。
回答by user2244112
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 ...
在最近版本的 facebook sdk 中,由于安全原因,facebook 禁用了让您访问某些朋友列表的功能……查看文档以了解更多信息……
回答by rampr
If you want to use the REST end point,
如果要使用 REST 端点,
$friends = $facebook->api(array('method' => 'friends.get'));
else if you are using the graph api, then use,
否则,如果您使用的是图形 API,则使用,
$friends = $facebook->api('/me/friends');
回答by Michael Vain
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.
改用 FQL,它类似于 SQL,但适用于 Facebook 的数据表,并且可以轻松涵盖您想要进行的数据查询。您不必使用所有这些 /xx/xxx/xx 调用,只需知道您感兴趣的表和列。
$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/
http://developers.facebook.com/docs/reference/fql/ 上很棒的交互式示例
回答by ShivarajRH
Using CodeIgniter OAuth2/0.4.0 sparks,
in Auth.php file,
使用 CodeIgniter OAuth2/0.4.0 sparks,
在 Auth.php 文件中,
$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,
并在 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.
回答by Deepak Kumar
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>';
回答by allyson
header('Content-type: text/html; charset=utf-8');
header('Content-type: text/html; charset=utf-8');
input in your page.
在您的页面中输入。