2011-12-15 18 views
1

我想弄清楚如何正確實現應用程序請求的朋友選擇器對話框。使用PHP SDK實現Facebook朋友選擇器對話的問題

我的目標是,一旦用戶進入我的競爭應用程序,如果他們不贏,他們可以選擇發送請求給5個朋友,然後他們將有機會進入。

我不知道是否有可能強制最低的5個朋友來進行選擇,但對於控制他們是否可以進入或不將存儲在數據庫中的某些數據的控制邏輯,即一旦請求被髮送,更新數據庫以允許他們重新輸入。

我跟着這個問題的代碼:

How to display the friends selector dialog with PHP sdk for Facebook?

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'XXXXXXXXX', 
    'secret' => 'XXXXXXXXXXXXXXXXXX', 
)); 

$user = $facebook->getUser(); 

$url = 'https://www.facebook.com/dialog/'; 
$url .= 'apprequests?app_id=XXXXXXXXXX&redirect_uri=http://www.domain.com/'; 
$url .= '&message=Share%20with%205%20friends%20for%20another%20chance%20to%20win!&display=popup'; 
?> 
<a href="<?php echo $url; ?>">Recommend friends for another chance to win!</a> 

<?php 

echo $_GET['request_ids']; 

if (isset($_GET['request_ids'])) { 
    for ($i=0; $i<count(request_ids); $i++){ 
     $link = ($link + "&to=" + $request_ids[$i]); 
    } 
    echo "<script language=javascript>parent.location=''</script>"; 
} 

我遇到的問題是,當我點擊鏈接,Facebook的標誌然後出現「去facebook.com」下。當我點擊它時,對話框會打開一整頁。

如果我點擊取消,它會帶我到我的域名,但它可以不重定向到標籤?

同樣,如果我完成應用程序請求對話框,我將被重定向到我的主頁,而我寧願將其重新定向到選項卡。

難以讓我的頭在這個幫助將不勝感激。

總而言之,我希望在彈出窗口中打開對話框,而不是突然出現Facebook標誌,然後在頁面中打開對話框。

然後,如果用戶點擊對話的「取消」以簡單地關閉並且如果請求完成,則對話再次關閉,而不是對話是整頁而是重定向到我的域。

謝謝。

回答

1

使用Facebook JS-SDK將提供您所要求的最佳體驗。如果這是一個選項(想不出爲什麼它不會),那麼你就應該使用Requests Dialog

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:fb="https://www.facebook.com/2008/fbml"> 
    <head> 
    <title>Request Tester C</title> 
    </head> 

    <body> 
    <div id="fb-root"></div> 
    <script src="http://connect.facebook.net/en_US/all.js"></script> 
    <p> 
     <input type="button" 
     onclick="sendRequestToRecipients(); return false;" 
     value="Send Request to Users Directly" 
     /> 
     <input type="text" value="User ID" name="user_ids" /> 
     </p> 
    <p> 
    <input type="button" 
     onclick="sendRequestViaMultiFriendSelector(); return false;" 
     value="Send Request to Many Users with MFS" 
    /> 
    </p> 

    <script> 
     FB.init({ 
     appId : 'YOUR_APP_ID', 
     status : true, 
     cookie : true, 
     oauth: true 
     }); 

     function sendRequestToRecipients() { 
     var user_ids = document.getElementsByName("user_ids")[0].value; 
     FB.ui({method: 'apprequests', 
      message: 'My Great Request', 
      to: user_ids, 
     }, requestCallback); 
     } 

     function sendRequestViaMultiFriendSelector() { 
     FB.ui({method: 'apprequests', 
      message: 'My Great Request' 
     }, requestCallback); 
     } 

     function requestCallback(response) { 
     // Handle callback here 
     } 
    </script> 
    </body> 
</html> 

如何處理髮送的文檔中,也以我的教程還介紹了要求;下面是與新的要求的格式(我使用jQuery)處理回調的例子:

function sendRequest() { 
    FB.ui({ 
     method: 'apprequests', 
     message: 'I want to give you this flower!', 
     title: 'Give a flower to some of your friends', 
     data: '{"item_id":1254,"item_type":"plant"}' 
    }, 
    function (response) { 
     if (response.request && response.to) { 
      var request_ids = []; 
      for(i=0; i<response.to.length; i++) { 
       var temp = response.request + '_' + response.to[i]; 
       request_ids.push(temp); 
      } 
      var requests = request_ids.join(','); 
      $.post('handle_requests.php',{uid: <?php echo $user; ?>, request_ids: requests},function(resp) { 
       // callback after storing the requests 
      }); 
     } else { 
      alert('canceled'); 
     } 
    }); 
    return false; 
} 

UPDATE:至於網友要求「最低限度」#。 JS-SDK對話框有一個max_recipients屬性,但沒有最小值,所以你需要有你自己的朋友選擇器,然後將to屬性設置爲這些朋友的ID。

+0

謝謝易卜拉欣,非常感謝。我最終使用JS SDK工作,並添加了一些代碼發佈在選定的每個朋友的牆上。 (從你的其他答案得到這個)。現在幾乎完成了應用程序,但如果你在附近,我可能會很快再次需要你的幫助! – martincarlin87 2011-12-16 11:14:14

相關問題