Wednesday , 22 November 2017

Develop Facebook App Like Love Calculator

Develop Facebook App Like Love Calculator

Do you know how to make a basic Facebook app just to show the text? If yes! then you are welcome to read rest of the post but if you are completely a beginner in Facebook app developing then you must read my old post to develop Facebook app first you have to learn the basic then you can develop more advance apps like today in this post we are going to learn that how you can develop a Facebook app to show and post love percentage of the user who is using your app so this is not going to be a very difficult tutorial just relax and listen any Avril Lavigne‘s song because I really love her voice :P like you can listen I’m With You which is really a great song, so let’s start the tutorial.

First Learn Basic Facebook App Development

The very first thing you have to do is to follow all the steps that is written in this post:

In that post everything is completely described that how to develop a basic app which can show the name of the user who is using your app. So if you have understood the basic structure that how it works to show user’s name if yes then you are ready to develop advance FB apps.

Develop Facebook App Like Love Calculator

Now it’s time to develop Facebook app which can show user’s love precentage and post on his/her wall with the message defined in the script. Remember we are suing PHP SDK means we are going to code app in PHP language.

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
<!DOCTYPE html>
<html>
	<head>
		<title>Love Calculator by Roomi</title>
	</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/et_blog/";
    $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;
    }
 
    $random_number = rand(0, 100);
    echo 'Your love percentage is '.$random_number.'%';
    $facebook->api('/me/feed', 'post', array('message' => 'Your love percentage is '.$random_number.'%', picture=> 'http://i2.lulzimg.com/cb491a748e.jpg', caption=> 'Developed by Roomi', link=> 'http://apps.facebook.com/lvcalculator/', description => 'Use this app to find out your love percentage.'));
 
?>
</body>
</html>

The code above is very easy to understand that how it works first if you have read my previous post about making basic Facebook app then you can understand the code above without any trouble. On line number 80 I used Facebook API (Application Programming Interface) to post on logged in user’s wall, it is simple just edit this line with the text you like, right now if you are using the code above then your app will be show user’s love percentage and post on user’s timeline. Remember to put your App ID and secret key from developers.facebook.com/apps if you need more help then comment below and more tutorials on Facebook app development are coming with more advance working and programming so get ready!

Leave a Reply