drl92080 2011-12-15 14:38
浏览 24
已采纳

使用PHP SDK实现Facebook好友选择器对话的问题

I am trying to figure out how to properly implement the friends selector dialogue for app requests.

What I am aiming for is, once the user has entered my competition app, if they don't win they can choose to send a request to 5 friends and then they will get another chance to enter.

I have no idea if it's possible to enforce a minimum of 5 friends to be selected but the logic for controlling whether they can enter or not will be controlled by some data stored in the database, i.e once the request is sent, update the db to allow them to re-enter.

I followed the code in this question:

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

The problems I am having are that when I click the link, the facebook logo then appears with 'go to facebook.com' underneath. When I click this, the dialogue opens in a full page.

If I click cancel, it takes me to my domain but can it not redirect to the tab?

Likewise, if I complete the app request dialogue I am redirected to my homepage when I would rather be re-directed to the tab.

Having a hard time getting my head around this so help would be greatly appreciated.

To summarise, I would like to have the dialogue open in a popup, rather than the facebook logo suddenly appearing and then the dialogue opening in the page.

Then, if the user clicks 'cancel' for the dialogue to simply close and if the request is completed, for the dialogue to close again, leaving the tab instead of the dialogue being a full page and redirecting to my domain.

Thanks.

  • 写回答

1条回答 默认 最新

  • doumu6997 2011-12-16 10:45
    关注

    Using the Facebook JS-SDK would provide the best experience you are asking for. If that's an option (can't think why it won't be), then you should use the 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>
    

    How to handle the requests sent is also described in the documentation and also in my tutorial; below is an example of handling the callback with the new requests format (I'm using 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: As for the "minimum" # of friends requirement. The JS-SDK dialog has a max_recipients property but no minimum, so you need to have your own friend selector then set the to property to those friends' ids.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP