2011-06-29 16 views
2

我想使用fb.api發佈登錄的用戶,但只是一次。如果我把這個Fb.api僅在登錄時發佈到用戶牆

var params = {}; 
params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.'; 
params['name'] = 'gegeggeeg - gegegege'; 
params['description'] = 'Check out Tegegegeg! Win merch by playing and reccomending to your friends.'; 
params['link'] = 'http://www.bblblba.com'; 
params['picture'] = 'http://summer-mourning.zoocha.com/uploads/thumb.png'; 
params['caption'] = 'Tgegegegeeg'; 

FB.api('/me/feed', 'post', params, function(response) { 
    if (!response || response.error) { 
    alert('Error occured'); 
    } else { 
    alert('Published to stream - you might want to delete it now!'); 
    } 
}); 

它發佈到用戶牆每次他刷新網站?

怎麼辦?

回答

1

什麼是觸發FB.api調用?如果它只是代碼中的代碼,那麼只要瀏覽器到達該點就會運行。

您可能會在FB.api調用後存儲某種cookie值或某些內容,然後在頁面加載時檢查它,但似乎需要更多的工作。

1

你想讓他只發布一次嗎?

如果是這樣,你將需要創建一個「狀態」。爲了做到這一點,你可以做到客戶端(使用cookie)或服務器端(使用數據庫)。

創建一個名爲「posted」的布爾變量,並將其存儲在cookie或數據庫中(因爲您使用的是javascript,因此可能更容易使用cookie)。 setCookie方法和getCookie方法的

var posted=getCookie("posted"); 
if(!posted) 
{ 
    //call the FB.api(); 
    setCookie("posted", true, duration); 
} 

定義:http://www.w3schools.com/JS/js_cookies.asp

+0

我寧願看看是否有可能做到沒有cookie?比方說,我需要在確切時刻貼在用戶的牆上,他點擊按鈕以允許我的應用在牆上張貼。所以當他接受我的比賽並且第一次比賽時,我想貼在他的牆上。我在這裏錯過了什麼嗎?我應該這樣做嗎? – Markus

+0

在這種情況下,你將不得不添加一個事件到那個將執行你的代碼的按鈕(作爲一個函數)。 但是,如果用戶多次玩遊戲,你會在每次玩遊戲時貼到牆上?如果不是,你需要創建一個「狀態」作爲某個點。使用這種方法,最好創建狀態服務器端並跟蹤您已發佈的牆,並且不要再發布它們。 – Jim

0

您可以運行FQL查詢和檢查,看看如果消息已經發布與應用ID查詢流表。就像:

<!DOCTYPE html> 
<html> 
<body> 
<div id="fb-root"></div> 
<a href="#" onclick="postToWall();return false;">Post To Wall</a> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
    FB.init({ appId : '**yourAppID**', status : true, cookie : true, xfbml : true }); 

    function postToWall() { 
    FB.login(function(response) { 
     if (response.session) { 
     FB.api(
      { 
      method: 'fql.query', 
      query: 'SELECT post_id, message from stream where app_id = **yourAppID** and source_id = me()' 
      }, 
      function(response) { 
      if(response.length == 0){ 
       FB.ui(
       { 
       method: 'feed', 
       name: 'Facebook Dialogs', 
       link: 'https://developers.facebook.com/docs/reference/dialogs/', 
       picture: 'http://fbrell.com/f8.jpg', 
       caption: 'Reference Documentation', 
       description: 'Dialogs provide a simple, consistent interface for applications to interface with users.', 
       message: 'Facebook Dialogs are easy!' 
       }, 
       function(response) { 
       if (response && response.post_id) { 
        alert('Post was published.'); 
       } else { 
        alert('Post was not published.'); 
       } 
       } 
      );   
      } 
      else { 
       alert('User already posted this message'); 
      } 
      } 
     ); 
     } 
    } , {perms:''}); 
} 
</script> 
</body> 
</html>