在进行api通话之前检查facebook帖子是否存在

时间:2014-04-27 10:54:20

标签: php facebook facebook-graph-api facebook-php-sdk fatal-error

我在Facebook上删除的帖子上进行api调用时遇到致命的php错误:"Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /facebook-php-sdk/src/base_facebook.php on line 1340"。我希望能够从我的数据库中删除帖子,如果该帖子已从Facebook删除,并删除此致命错误。我的api调用是在一个while循环中返回顶部帖子(在评论,分享和喜欢上排名)。我需要在返回帖子之前检查帖子是否存在,如果从Facebook中删除,则从数据库中删除它。

$get_content_db_info = mysqli_query($dbc , "SELECT * FROM `content`");
echo '<table id="leaugeTable"><thead><tr><th>Name</th><th>Post ID</th><th>Facebook ID</th><th>Score</th></tr></thead><tbody>';
while($row = mysqli_fetch_array($get_content_db_info)){
    // then your for loop
    $fbApiGetPosts = $facebook->api('/'.$row["fb_id"].'_'.$row["post_id"]);
    $fbApiGetShares = $fbApiGetPosts["shares"];
    $shareCount = $fbApiGetShares["count"];
    $fbApiGetLikes = $facebook->api('/'.$row["post_id"].'/likes');
    $countLikes = $fbApiGetLikes["data"];
    $likesResult = count($countLikes);
    $fbApiGetComments = $facebook->api('/'.$row["post_id"].'/comments');
    $countComments = $fbApiGetComments["data"];
    $cc = count($countComments);
    $score = $likesResult + $shareCount * 2 + $cc * 3;
    echo '<tr><td>'.$row["first_name"].' '.$row["last_name"].'</td><td>'.$row["post_id"].'</td><td>'.$row["fb_id"].'</td><td class = "sortCol">'.$score.'</td></tr>';
}

这是我的第一个facebook项目,不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

如果帖子已从Facebook中删除,您会看到Fatal error: Uncaught GraphMethodException: Unsupported get request.错误。你能做的是:

while($row = mysqli_fetch_array($get_content_db_info)){
  try {
    // your original while code
  } catch ( Exception $e ) {
    // error from facebook, post is likely to be deleted
    if ( $ex->getMessage() === 'Unsupported get request.' ) {
      // delete post from database
    }
  }
}

try ... catch语句将捕获任何错误,包括Unsupported get request.,然后您可以从数据库中删除帖子,而不会影响其余的流程。