Monday , 24 July 2017

How to Invite Friends Using Facebook API

Facebook API: Invite Friends Button

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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!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.

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 ;)

Leave a Reply