Saturday , 25 October 2014

Facebook API: Post on Timeline Using PHP SDK

Hi everyone, I am 101% sure that you have an account on Facebook maybe two accounts or more :P because it is the most famous and powerful social network yet. You must be here to learn that how you can post on Facebook user’s timeline using Using Facebook API with the help of PHP SDK because that’s what this article is about :D So I am not going to share that how you can setup an app on Facebook because I have already written about it but also I have written more on Facebook app development so you should read those articles too.
Post on Timeline Using Facebook API

Only for PHP Programmers!

So first let me tell you that the only way that I know to post on user’s timeline is by using PHP SDK because I don’t have any experience in other programming languages like C#, Java etc. So if you are a PHP programmer then you should keep reading it otherwise first you should learn PHP then read this article :P just kidding :)

You Know Basics of Facebook App Development!

So the first thing you need to know is that you should know the basics of Facebook App Development, if you do not have any knowledge that how to make a basic Facebook app using PHP + Facebook API then read my previous article about making basic FB app after reading that article now you can proceed with this article.

Let’s Make App to Post on Timeline Using Facebook API

Now we should start, the code is given below and most of the part of the given code is just basic information about the app and making a connection between your app files and Facebook API. So just take a look at the code below:

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
<!--?php 
 
    $fbconfig['appid' ] = "APP-ID-HERE"; // paste app id here
    $fbconfig['secret'] = "APP-SECRET-HERE"; // paste app secret code here
    $fbconfig['baseUrl']    =   "http://roomi.orgfree.com/etb/"; // type the exact app location as URL
    $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/extreme_techblog/"; // type the app URL given by Facebook
 
    /* 
     * 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 
     * -mahmud
     */
    if (isset($_GET['code'])){
        header("Location: " . $fbconfig['baseUrl']);
        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' =&gt; $fbconfig['secret'],
      'cookie' =&gt; true,
    ));
 
    //Facebook Authentication part
    $user       = $facebook-&gt;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-&gt;getLoginUrl(
            array(
                'scope'         =&gt; 'publish_stream'
            )
    );
 
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook-&gt;api('/me');
        $access_token = $facebook-&gt;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;
    }
 
        $statusUpdate = $facebook-&gt;api('/me/feed', 'post', array(message=&gt; 'I have liked this page and I recommend you to do the same.', picture=&gt; 'http://profile.ak.fbcdn.net/hprofile-ak-prn1/s160x160/58264_520790901276624_1042098307_a.jpg', link=&gt; 'http://www.facebook.com/extremetechblog'));
 
        echo "Welcome ".$user_profile['name']." to Extreme Tech Blog's official Facebook app.";
 
?&gt;
  • First of all replace the app id and app secret with your app id and secret code on line numbers 3 and 4.
  • As I wrote above that most of the code is same means the basics so first learn some basics that how to make a FB app blah blah.
  • Now look line number 53 I got the permission to post on user’s timeline who will be using my app and the permission we need is publish_stream so first we must get it.
  • On line no. 61 I got access token which is required if you are going to use Facebook API.
  • Now see line number 74 the most important line of code because this is where I added code to post on the user’s timeline who is using my app. So you just have to change the message which will be posted on timeline and also change the picture link which will be displayed as thumbnail and you can also add a link to this status update.
  • Line number 76 tells us that when user will open the app then he will see his/her name with a welcome message.

After adding all the details now you just have to ask any of your friend to go your app  and allow your app on Facebook and before doing that first make sure that your app is not in sandbox mode if it is then only app developers can use that app so first go to developers.facebook.com/apps/ and select your app and in the basic details settings disable the sandbox mode. So After a user will open your Facebook app then his/her status will be updated as described in the code.
Post on Timeline Using Facebook API

Have Any Question?

If you got any question about this tutorial then please reply and try to explain everything that you want to ask and I will reply you as soon as possible with the solution to your problem and don’t forget it share it with your friends who are PHP programmers :)

Leave a Reply