How to Invite Friends Using Facebook API

I am back with another article actually tutorial to help those who are beginner in Facebook app development I have already written few articles on this topic so in this post I am going to share that how you can use Facebook API to make a button on your app page to invite friends, it will help your app to get many users and your app might go viral on Facebook in very short time if it’s a really useful app, so you must learn this.

Read Also: How to Get Unblocked from Posting on Facebook?

Facebook API: Invite Friends Using JavaScript SDK
I am not going to cover all steps like how to create an app from scratch or how to create basic app because I have already covered all basics about Facebook app development so now I am going to post code below and I am going to describe it that how it works. And also read my another article to make a Facebook app which can post on all groups at once, I’m sure you will love it.

Read Also: How to Delete Facebook Account Permanently Immediately in Android Device

<!DOCTYPE html>
<html>
<head>
<title>Developed by Roomi</title>
<script type=”text/javascript” src=”http://connect.facebook.net/en_US/all.js”></script>
<script type=”text/javascript”>
FB.init({
appId : ‘APP ID HERE’,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

function newInvite(){
var receiverUserIds = FB.ui({
method : ‘apprequests’,
message: ‘Invite your Facebook friends to use this app.’,

},
function(receiverUserIds) {
console.log(“IDS : ” + receiverUserIds.request_ids);
}
);
//http://developers.facebook.com/docs/reference/dialogs/requests/
}
</script>
</head>
<body>

<?php
$fbconfig[‘appid’ ] = “APP ID HERE”;
$fbconfig[‘secret’] = “APP SECRET HERE”;

$fbconfig[‘baseUrl’] = “URL TO EXTRACT DATA FOR APP”; // “http://roomi.orgfree.com/etb/”;
$fbconfig[‘appBaseUrl’] = “URL PROVIDED BY FACEBOOK”; // “http://apps.facebook.com/et_blog/”;

/*
* If user first time authenticated the application facebook
* redirects user to baseUrl, so I checked if any code passed
* then redirect him to the application url
*/
if (isset($_GET[‘code’])){
header(“Location: ” . $fbconfig[‘appBaseUrl’]);
exit;
}
//~~

//
if (isset($_GET[‘request_ids’])){
//user comes from invitation
//track them if you need
}

$user = null; //facebook user uid
try{
include_once “src/facebook.php”;
}
catch(Exception $o){
print_r($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
‘appId’ => $fbconfig[‘appid’],
‘secret’ => $fbconfig[‘secret’],
‘cookie’ => true,
));

//Facebook Authentication part
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

$loginUrl = $facebook->getLoginUrl(
array(
‘scope’ => ‘publish_stream’
)
);

if ($user) {
try {
// Proceed knowing you have a logged in user who’s authenticated.
$user_profile = $facebook->api(‘/me’);
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}

if (!$user) {
header(‘location: ‘.$loginUrl);
exit;
}

echo ‘<button onclick=”newInvite()”>Invite your friends!</button>’;

?>
</body>
</html>
On line number 8 just put your app ID (I hope you know what I am talking about).
See code from line 14 to 23, this code will create a new function newInvite() we can call this function by creating a button, yes it is so simple to do so.
Now see line number 100 you can see there I added a line of code to show a button on app page and I added a functionality with JavaScript that if you click this button it will call the function newInvite() which is written between the <head></head> tags.

Read Also: How Do I Fix Facebook Login Error Issue?

So I hope you will understand the rest of the code because as I said I have already written few articles on basic Facebook app development, you can search on blog for Facebook app development and if you need any help regarding this article then please ask anything in the comments I will reply you as fast as I can, read my another article to make Facebook app like love calculator and thanks for reading and sharing 😉

Recommended: Get Facebook Spy to Build a Healthy Relationship With Your Kid

Leave a Comment