Facebook api邀请朋友

时间:2011-12-28 08:09:58

标签: facebook facebook-graph-api

我有一个基于php的网站www.darooadda.com。我想知道是否有任何Facebook API邀请Facebook帐户的朋友。如果用户使用Facebook帐户登录网站,他应该能够从网站邀请他的Facebook好友。

任何人都可以帮助我使用正确的api文档或示例PHP代码吗?

2 个答案:

答案 0 :(得分:5)

此示例代码可以使用facebook API 3.0.1,请求朋友并在发送请求时回发到您的网站

$<?php
// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('includes/facebook.php');
define('FACEBOOK_APP_ID',"your facebook ID");
define('FACEBOOK_SECRET',"your facebook secret password");

$user = null;

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

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

if ( empty($user) ) {
echo("<script> top.location.href='" . $loginUrl . "'</script>");
exit();
}
?>
<!doctype html>
<html>
<head>
  <title>Send An Application Request Using The Facebook Graph API</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

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


<a href="#">Send Application Request</a>

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId: '<?php echo $facebook->getAppID() ?>',
      status: true,
      cookie: true,
      oauth: true
    });
  };

  $('a').click(sendRequest);
  function sendRequest() {
    FB.ui({
      method: 'apprequests',
      message: 'Check out this application!',
      title: 'Send your friends an application request',
    },
    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/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
  }(document));
</script> 
 </body>
</html>

答案 1 :(得分:2)

从您的网站邀请Facebook好友,使用这一小行代码并发送邀请给所有Facebook好友访问您的网站。我也使用这个脚本很好地工作。 您的域名必须 ssl认证,因为Facebook不允许不安全的域名。

<script src="http://connect.facebook.net/en_US/all.js"></script>

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

function FacebookInviteFriends()
{
FB.ui({
method: 'apprequests',
message: 'Your Message diaolog'
});
}
</script>


//HTML Code
<div id="fb-root"></div>
<a href='#' onclick="FacebookInviteFriends();"> 
Facebook Invite Friends Link
</a>

将以下代码添加到您的网站主页..

<script type='text/javascript'>
if (top.location!= self.location)
{
top.location = self.location
}
</script>

有关详细信息,请查看: - http://www.9lessons.info/2012/07/facebook-invite-friends-api.html