0

我创建了一个 Facebook PHP 应用程序,并尝试创建一个请求对话框。我使用了 SDK,但是当我点击邀请朋友时它没有出现。

这是代码:

    <?php
// PATH TO THE FB-PHP-SDK
require_once 'src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '475970839112689',
  'secret' => '4ae240ac95f687af581ff1596d06121d'
));

$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();

if ( empty($user) ) {
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
    exit();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
etc...

之后,我为对话框加载了 JS 并创建了一个链接:

    <div id="fb-root"></div>

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '475970839161689',
        status: true,
        cookie: true,
        oauth: true
    });
};

$('a').click(sendRequest);
function sendRequest() {
    FB.ui({
        method: 'apprequests',
        message: 'Vino si soializeaza si tu pe Social Chat!',
        title: 'Trimite prietenilor tai o invitatie',
    },
    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;
}

  // Load the SDK Asynchronously
(function(d){
 var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/ro_RO/all.js";
 d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

<a href="#">Invite friends</a>

所以,当我点击邀请朋友链接时,它什么也没做。

4

1 回答 1

0

只需将您的点击功能包装在准备好的功能中,如下所示:

$(document).ready(function() { 
   $('a').click(sendRequest);
});
于 2013-07-25T22:27:23.973 回答