如何在Facebook图表上获得给定用户的最新帖子?


How do you get the most recent posts from a given user on Facebook graph?

我正在尝试获取给定用户(php)的朋友的最后帖子
$Fb = new Facebook(array('appId'=>'xxxxxxxxxxxxxx','secret'=>'xxxxxxxxxxxxxx '));
$access_token = $Fb->getAccessToken();
//我得到一个token
url = " https://graph.facebook.com/"。美元id。"/帖子? access_token = " $ access_token;
$data = file_get_contents($url);//与其他命令一起使用
$var = json_decode($data, true);//作为数组,而不是对象
$txt .= display_tree($var);//我自己的方式显示
没有结果。

  • 使用Facebook PHP SDK从用户那里获得授权,并使用"scope"方法获得"read_stream"权限,这将有可能看到用户的个人资料流,更被称为"墙",但是,因为你不想看到那里的朋友更新,你可以要求"offline_access"权限,然后,在用户登录Facebook后,网站将返回数据给你。有用户的墙帖和主帖(主帖包含用户订阅的数据,所以会有好友帖和其他页面帖)。

然后,一旦你有权限"offline_access",你可以在任何时候获得数据,使用用户访问令牌,从该权限。

这个文件,检查用户是否在Facebook在线,如果没有,他们将被带到Facebook登录页面,如果用户还没有给予许可,他们将被要求。loginURL后面的"scope"是你想要的权限。


<?php
require 'facebook.php'; // Make sure to have the Facebook PHP SDK
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APPID',
  'secret' => 'APPSECRET',
));
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get User ID
$user = $facebook->getUser();
/* We may or may not have this data based on whether the user is logged in.
   If we have a $user id here, it means we know the user is logged into
   Facebook, but we don't know if the access token is valid. An access
   token is invalid if the user logged out of Facebook. */
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    // These are the graph calls -->
    $dt = $facebook->api('/me');
    $lk = $facebook->api('/me/likes');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Handler for Login Status
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array("scope" => "email,user_birthday,user_likes,user_location,offline_access,read_stream"));
}
// ----------------------------------------------------------------------------------------
?>
<?php if (!$user): header ('Location:'.$loginUrl.''); //checks if the user is logged in
else: 
        // Do Something here. This next bit of code shows what comes out of those calls.
      echo "<pre>"; 
      print_r($dt);
      echo"</pre>";
      echo"<br/><br/>";
      echo "<pre>"; 
      print_r($lk);
      echo"</pre>";
endif
?>

首先在Facebook的图形资源管理器工具中尝试一下,这样您就可以将问题与php代码中的任何问题隔离开来。

https://developers.facebook.com/tools/explorer

看看你的代码,看起来你没有要求read_stream权限?仅仅一个访问令牌将只能获得您的公开帖子,也许有问题的用户没有公开帖子。

http://developers.facebook.com/docs/reference/api/user/

可能是Facebook给出的错误,你无法读取,因为默认情况下你无法读取错误页面。

file_get_contents()需要有一个给定的stream_context(第三个参数),表示'ignore_error' => TRUE

否则你无法看到Facebook作为json给你什么样的错误,函数只会返回FALSE。

stream_context_create ()