Invite Friends with Facebook using PHP SDK and Javascript SDK

 

Invite Friends with Facebook is developed to send the invitation requests to the user’s friends. For this module we can use PHP SDK 3.0 and JavaScript SDK.
For Creating an App, which you can obtain from the App Dashboard.
In this demo module,You need App Id and App Secret.

Live DemoLive Demo

Download Script

config.php

<?php
session_start();
$appID='Your APP Id';
$appSecret='Your APP Secret';
if($_SERVER['HTTP_HOST']=='localhost'){
$base_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}else{
$base_url='http://'.$_SERVER['HTTP_HOST'];	
}
?>

Authentication & Authorization

Authentication functionality is can be achieved using JavaScript SDK method which is FB.login.

index.php

<?php require 'config.php';  ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Invite Friends using Facebook</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
</head>

<body>

<img src="images/invite_facebook.png" id="facebook"  style="cursor:pointer;float:left;margin-left:460px;" />

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

   </script>
   <script type="text/javascript">
  window.fbAsyncInit = function() {
     FB.init({ 
       appId:'<?php echo $appID; ?>', cookie:true, 
       status:true, xfbml:true,oauth : true 
     });
   };
   (function(d){
           var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
           if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           ref.parentNode.insertBefore(js, ref);
         }(document));
 $('#facebook').click(function(e) {
    FB.login(function(response) {
	  if(response.authResponse) {
		  parent.location ='<?php echo $base_url; ?>invite.php';
	  }
 },{scope: 'email,read_stream,publish_stream,user_birthday,user_location,user_work_history,user_hometown,user_photos'});
});
   </script>
</body>
</html>

And for get the list of Facebook friends, we used the invite.php

invite.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Invite Friends using Facebook</title>
<style type="text/css">

.fb_frnds{
	list-style:none;
}
.fb_frnds li{
		padding:10px;
	float:left;
	width:30%;
}
.frnd_list{
	margin-top:-25px;
	margin-left:40px;
}
.fb_frnds a{
	text-decoration:none;
	background: #333;
        /* for IE */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#333', endColorstr='#D95858'); 
        /* for webkit browsers */ 
        background: -webkit-gradient(linear, left top, left bottom, from(#333), to(#D95858)); 
        /* for firefox 3.6+ */ 
        background: -moz-linear-gradient(top,  #333,  #D95858);
        color: #FFFFFF;
	float: right;
	font: bold 13px arial;
	margin-right:110px ;
}
</style>
</head>
<body>
<?php 

require 'config.php';
require 'lib/facebook/facebook.php';

$facebook = new Facebook(array(
		'appId'		=>  $appID,
		'secret'	=> $appSecret,
		));
//get the user facebook id	

$user = $facebook->getUser();

if($user){
	try{
		//get the facebook friends list
	  $user_friends = $facebook->api('/me/friends');
	}catch(FacebookApiException $e){
		error_log($e);
		$user = NULL;
	}		
}
if(isset($user_friends)){  ?>
<h1> Facebook Friends List </h1>   <a href="javascript:void(0);" onclick="fb_logout();">Logout</a>
<ul  class="fb_frnds">
<?php
	foreach($user_friends['data'] as $user_friend){
?>
<li ><img src="https://graph.facebook.com/<?php echo $user_friend['id']; ?>/picture" width="30" height="30"/>
<div  class="frnd_list"><?php echo $user_friend['name']; ?><a href="javascript:void(0);" onclick="send_invitation(<?php echo $user_friend['id']; ?>);"> Invite </a></div>
</li>

<?php  }  ?>
</ul>
<?php
}else{

header('Location: '.$base_url);	
}?>
   <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"> </script>

<script type="text/javascript">
FB.init({ 
       appId:'<?php echo $appID; ?>', cookie:true, 
       status:true, xfbml:true 
     });

function send_invitation(fb_frnd_id){
     FB.ui({ method: 'apprequests', 
       message: 'IdiotMinds Programming Blog...',
	   to:fb_frnd_id
     });
}
function fb_logout(){
	FB.logout(function(response) {
		  parent.location ='<?php echo $base_url; ?>';
});

}
</script>
</body>
</html>

16 Comments

  1. car wrap st albans

    I’m now not sure the place you are getting your information, but great topic. I needs to spend some time learning much more or figuring out more. Thank you for fantastic information I was on the lookout for this information for my mission.

  2. Lose Belly Fat

    Hi, Neat post.

  3. Thank you very much for that excellent article

  4. Herlina

    You’re the getreast! JMHO

  5. I have tried this. Quit simple and efficient. Can you tell me gmail and twitter invitations?

  6. Sanjay Chauhan

    invite not working.
    When we invite facebook friend than nothing happen and request are not sending to friends wall notification.

Leave a Reply