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.

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集