如何从帐户获取Facebook状态

时间:2014-04-24 06:07:54

标签: php facebook-graph-api

我正在尝试从其Feed中获取Facebook用户状态。这是我的代码:

<?php
  require_once('facebook.php');

  $config = array(
    'appId' => 'my_app_id',
    'secret' => 'my_app_secret',
    'allowSignedRequest' => false
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();//get user_id here...
?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {
      try {

        $status = $facebook->api('/me/posts?limit=1');//get posts here...
        echo "The status: " . $status['message'];//print the message of posts...

      } catch(FacebookApiException $e) {
        $params = array(
        'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
        'redirect_uri' => 'http://localhost/facebook/index.php', // Replace with your app url
    ); //permission = read_stream, user_status
        $login_url = $facebook->getLoginUrl($params);
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

  </body>
</html>

但结果是空的。谁能告诉我如何解决这个问题?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

您正在以错误的方式阅读返回的数据,应该是;

$response = $facebook->api('/me/posts?limit=1');//get posts here...
$status = null;
foreach($response["data"] as $item) {
    $status = $item["message"];
}
echo "The status: " . $status;

您可以针对返回的数据格式测试查询here