如何將用Facebook評論發表的評論保存至mysql數據庫?我希望能夠搜索其他頁面上的評論。將facebook評論保存至mysql數據庫
回答
你的意思是你評論的地方,例如你的牆,註釋到你的mysql數據庫?
如果是這樣,你需要學習Facebook的API(圖形API)
<?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>';
}
}
}
?>
這取得fb數據...從這裏你可以改進 – Master345
有幾種方法可以做到這一點。
第一種方法是一次性獲得所有評論。您必須定期執行以獲取新評論,並避免重複數據庫中的舊評論。
這可以通過你的網頁的URL訪問圖形API來實現:
https://graph.facebook.com/comments/?ids=http://example.com/your_page
這個方法返回JSON,你必須分析評論。如果太多,會有一個「分頁」散列,告訴你下一頁的地址。
第二種方法是跟蹤新評論並立即保存。這可以避免重複發生的重複問題。這將需要使用Javascript and Facebook js events。
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)。
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
- 1. 如何將Facebook評論保存到Mysql數據庫?
- 2. 評論不保存到數據庫
- 3. 保存檔案評論至ePub文件
- 4. MySql數據庫設計評論系統
- 5. Facebook的數據庫結構存儲評論和發佈?
- 6. 將WordPress評論與Facebook評論合併
- 7. Facebook的評論數
- 8. Rails應用程序評論不保存到數據庫
- 9. MySQL數據庫來存儲用戶評論
- 10. Facebook評論:全部評論
- 11. Facebook Connect評論是否存儲在本地或Facebook的數據庫?
- 12. 已移至代碼評論評論
- 13. MySQL將文件保存到數據庫
- 14. PHP MySQL:將PDF保存到數據庫
- 15. 將圖保存到MySQL數據庫
- 16. 將JPanel保存到mySQL數據庫(Java)
- 17. 爲什麼我的論壇中的評論沒有保存到數據庫?
- 18. 保存清單至數據庫
- 19. Facebook的評論
- 20. Facebook的評論
- 21. 將MySQL數據庫從Linux移至Mac?
- 22. facebook評論數錯了
- 23. 評論集數與Facebook API
- 24. 將照片保存至核心數據
- 25. checkins /最近沒有返回評論,甚至沒有評論數
- 26. 數據保存到MySQL數據庫
- 27. Facebook的評論意見,評論
- 28. 重複評論Facebook的評論框
- 29. Facebook Like button評論框,空的評論
- 30. 加載facebook評論框沒有評論
對不起,我應該更好地解釋。我在我的網站上的一個頁面上放置了Facebook評論,我想在我的網站上搜索這些評論。我查看了Facebook API,可以在我的網站上的頁面上顯示評論圖表,但尚未找到如何使用/訪問圖表中的數據。 – cbr0wn
因此,對於我所瞭解的你有一個facebook.com/pageid頁面,並且可以從你的www.yoursite.com搜索該facebook頁面的所有評論,是嗎?如果是這樣,我真的不知道該怎麼做,但一開始它會使用Graph API將您的fb頁面中的所有註釋都回顧到數據庫中,並且首先要創建一個FB應用程序... – Master345
對不起,讓我解釋得更好。我有一個我擁有的Facebook評論iframe的服務器上的頁面。我已經做了一個Facebook應用程序。我可以在屏幕上顯示評論的圖形數據,但我不知道如何保存或將它們存儲到變量或更有用的東西,然後在屏幕上顯示文本。 – cbr0wn