2011-08-14 60 views

回答

1

你的意思是你評論的地方,例如你的牆,註釋到你的mysql數據庫?

如果是這樣,你需要學習Facebook的API(圖形API)

+0

對不起,我應該更好地解釋。我在我的網站上的一個頁面上放置了Facebook評論,我想在我的網站上搜索這些評論。我查看了Facebook API,可以在我的網站上的頁面上顯示評論圖表,但尚未找到如何使用/訪問圖表中的數據。 – cbr0wn

+0

因此,對於我所瞭解的你有一個facebook.com/pageid頁面,並且可以從你的www.yoursite.com搜索該facebook頁面的所有評論,是嗎?如果是這樣,我真的不知道該怎麼做,但一開始它會使用Graph API將您的fb頁面中的所有註釋都回顧到數據庫中,並且首先要創建一個FB應用程序... – Master345

+2

對不起,讓我解釋得更好。我有一個我擁有的Facebook評論iframe的服務器上的頁面。我已經做了一個Facebook應用程序。我可以在屏幕上顯示評論的圖形數據,但我不知道如何保存或將它們存儲到變量或更有用的東西,然後在屏幕上顯示文本。 – cbr0wn

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>'; 
    } 
    } 
} 


?> 
+0

這取得fb數據...從這裏你可以改進 – Master345

0

有幾種方法可以做到這一點。

第一種方法是一次性獲得所有評論。您必須定期執行以獲取新評論,並避免重複數據庫中的舊評論。

這可以通過你的網頁的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)。

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 
+0

請添加一些解釋以及它會作出更好的答案。 – Szymon