将Facebook评论保存到MySQL数据库

3

我如何将使用Facebook评论功能所做的评论保存到MySQL数据库中?我想要能够在另一个页面上搜索这些评论。

4个回答

1
<?php

// displays some comments for a certain url
$url = 'http://developers.facebook.com/docs/reference/fql/comment/';

// fql multiquery to fetch all the data we need to display in one go
$queries = array('q1' => 'select post_fbid, fromid, object_id, text, time from comment where object_id in (select comments_fbid from link_stat where url ="'.$url.'")',
                 'q2' => 'select post_fbid, fromid, object_id, text, time from comment where object_id in (select post_fbid from #q1)',
                 'q3' => 'select name, id, url, pic_square from profile where id in (select fromid from #q1) or id in (select fromid from #q2)',
                 );

// note format json-strings is necessary because 32-bit php sucks at decoding 64-bit ints :(
$result = json_decode(file_get_contents('http://api.facebook.com/restserver.php?format=json-strings&method=fql.multiquery&queries='.urlencode(json_encode($queries))));

$comments = $result[0]->fql_result_set;
$replies = $result[1]->fql_result_set;
$profiles = $result[2]->fql_result_set;
$profiles_by_id = array();
foreach ($profiles as $profile) {
  $profiles_by_id[$profile->id] = $profile;
}
$replies_by_target = array();
foreach ($replies as $reply) {
  $replies_by_target[$reply->object_id][] = $reply;
}

/**
 * print a comment and author, given a comment passed in an an array of all profiles.
 * @param object $comment as returned by q1 or q2 of the above fql queries
 * @param array $profiles_by_id, a list of profiles returned by q3, keyed by profile id
 * @returns string markup
 */
function pr_comment($comment, $profiles_by_id) {
  $profile = $profiles_by_id[$comment->fromid];
  $author_markup = '';
  if ($profile) {
    $author_markup =
      '<span class="profile">'.
        '<img src="'.$profile->pic_square.'" align=left />'.
        '<a href="'.$profile->url.'" target="_blank">'.$profile->name.'</a>'.
      '</span>';
  }

  return
    $author_markup.
    ' ('.date('r', $comment->time).')'.
    ': '.
    htmlspecialchars($comment->text);
}

print '<html><body>';

// print each comment
foreach ($comments as $comment) {
  print
    '<div style="overflow:hidden; margin: 5px;">'.
      pr_comment($comment, $profiles_by_id).
    '</div>';
  // print each reply
  if (!empty($replies_by_target[$comment->post_fbid])) {
    foreach ($replies_by_target[$comment->post_fbid] as $reply) {
      print
        '<div style="overflow:hidden; margin: 5px 5px 5px 50px">'.
          pr_comment($reply, $profiles_by_id).
        '</div>';
    }
  }
}


?>

这会获取 Facebook 数据...从这里你可以改进。 - Master345

1

你的意思是在你的墙上评论时,评论如何到达你的MySQL数据库?

如果是这样,你需要学习Facebook API(Graph API)。


抱歉,我应该解释得更清楚。我在我的网站上放置了Facebook评论,我想要在我的网站上搜索这些评论。我已经研究了Facebook API,并可以在我的网站上显示页面上的评论图表,但是还没有找到如何使用/访问图表中的数据。 - cbr0wn
据我所了解,您拥有一个facebook.com/pageid页面,并且该Facebook页面的所有评论可以从您的www.yoursite.com进行搜索,对吗?如果是这样的话,我不是很清楚如何做,但是开始使用Graph API检索您的fb页面中的所有评论并将其存储到数据库中是一种方法。首先,您需要创建一个FB应用程序... - Master345
2
抱歉让我更好地解释一下。我拥有一个位于自己服务器上的页面,其中包含一个 Facebook 评论 iframe。我已经创建了一个 Facebook 应用程序。我可以在屏幕上显示评论的 graph 数据,但我不知道如何保存它们或将它们存储到变量或其他更有用的东西中,而不仅仅是在屏幕上显示文本。 - cbr0wn
你不能将图形数据存入数组,然后再存回数据库吗?我不明白问题出在哪里... - Master345
我不知道怎么做那个... -____- - cbr0wn

0
FB.Event.subscribe('comment.create',
function(response) {
onCommentCreate(response.commentID,response.href); //Handle URL on function to store on database

alert(response.href); //it gives you url
}
);

function onCommentCreate(commentID,href) {
    $.ajax({
        type: 'POST',
        url: 'handlecomment.php',
        data: {commentid:commentID,href:href},
        success: function(result)
        {
        alert(result);
        }
    });
}

//hadlecomment.php 


<?php 
error_reporting(E_ERROR);
  $commentid=$_POST['commentid'];
  $url=$_POST['href'];
  $pid=substr($url,strpos($url, 'comments')+8);

 // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/
  require_once('php-sdk/facebook.php');

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
 $accesstoken=$facebook->getAccessToken();

 if($user_id) {

  // We have a user ID, so probably a logged in user.
  // If not, we'll get an exception, which we handle below.
  try {
        $facebook->setAccessToken($accesstoken);
        $fql = 'SELECT text from comment where id = ' . $commentid;
        $ret_obj = $facebook->api(array(
                                   'method' => 'fql.query',
                                   'query' => $fql,));
$comment= $ret_obj[0]['text'] ;
     $insert_comment="insert into comments(pid,comment) values($pid,$comment)";
mysql_query($insert_comment);


  } catch(FacebookApiException $e) {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $login_url = $facebook->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  // No user, so print a link for the user to login
  $login_url = $facebook->getLoginUrl();
  echo 'Please <a href="' . $login_url . '">login.</a>';

}
  ?>

?>


//YOu need to set data-href of comment should be look like this...
//i am using more comments on my website so i looped through to add comment 


while($result=mysql_fetch_assoc(mysql_query($query)))
{
  $pic_id=$result['pic_id']; // i have saved unique pic id in my database for all images so  i am    

//retrieving that here

<div class="fb-comments" style=' position:relative;left:55px;top:10px;'  data-href="<?php echo 'http://www.lpuphotography.edulogics.in/votography.php/comments' . $pic_id; ?>" data-width="470" data-num-posts="2"></div>

}

//if you are using single comment

<div class="fb-comments" style=' position:relative;left:55px;top:10px;'  data-href="<?php echo 'http://www.lpuphotography.edulogics.in/votography.php/comments101'  ?>" data-width="470" data-num-posts="2"></div>

//101 is comment id , u can set what ever you want

请添加一些解释,这样回答会更好。 - Szymon

0

有几种方法可以实现。

第一种方法是一次性获取所有评论。您需要定期执行此操作以获取新评论,并避免在数据库中重复旧评论。

这可以通过使用页面的URL访问Graph API来实现:

https://graph.facebook.com/comments/?ids=http://example.com/your_page

这个会以 JSON 形式返回评论,你需要对其进行解析。如果评论很多,会有一个 'paging' 哈希告诉你下一页的地址。


第二种方法是跟踪新评论并立即保存它们。这避免了重新获取重复问题。这将需要使用Javascript和Facebook js事件
FB.Event.subscribe('comment.create', function(response) {
  var commentQuery = FB.Data.query('SELECT fromid, text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
  FB.Data.waitOn([commentQuery], function () {
    text = commentQuery.value[0].text;
    // Use your preferred way to inform the server to save comment
    $.post( 'http://example.com/comment', text )
  });
});

下面的示例在客户端获取评论。但是您也可以在服务器端执行此操作。
当然,您需要包含Facebook的JavaScript库,并在服务器上实现发布操作(http://example.com/comment)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接