Facebook API – Get friends activity

Facebook API – Get friends activity

0 38850
Facebook API - Get friends activity

Facebook API – Get friends activity

Today I want to show you how to display activity feeds of your friends at your website. Ever wondered what your friends were doing on the Facebook Pages that you both like? Most of you can answer ‘yes’. And today we are going to display that activity feed at your website, I am going to use Facebook API (using fql.query). The result script can show us different feed types, for instance: status update, posted links, notes, photos, videos and other. Everything are located in ‘stream’ FQL table. So, let’s look to the realization.

Firstly, we should create our own new application at Facebook. Please follow this link and click ‘Create New App’ button at the left top:

Faceboog Friends API - step 1

We should select our App Name and click ‘Continue’ button. We’ve just got our own api key:

Faceboog Friends API - step 2

Now, please pay your attention to Basic Info block, we should type our App Domains and Site URL params:

Faceboog Friends API - step 3

That’s all, click ‘Save Changes’ button, and lets start coding !


Live Demo

Step 1. PHP

Now, please create an empty index.php file and put next code:

index.php

<?php
$sApplicationId = 'YOUR_APPLICATION_ID';
$sApplicationSecret = 'YOUR_APPLICATION_SECRET';
$iLimit = 99;
?>
<!DOCTYPE html>
<html lang="en" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
    <meta charset="utf-8" />
    <title>Facebook API - Get friends activity | Script Tutorials</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script>
        google.load("jquery", "1.7.1");
    </script>
    <script type="text/javascript" src="js/jsrender.js"></script>
    <!-- define template for our units -->
    <script id="facebookTemplate" type="text/x-jsrender">
        <div id="{{:post_id}}" class="fbf">
            {{if actor_id}}
                <img src="https://graph.facebook.com/{{:actor_id}}/picture" />
            {{/if}}
            <div>
                {{if description}}
                    <p><a href="{{:permalink}}">{{:description}}</a> <span>(Type: {{:type}})</span></p>
                {{/if}}
                {{if message}}
                    <p><a href="{{:permalink}}">{{:message}}</a> <span>(Type: {{:type}})</span></p>
                {{/if}}
            </div>
        </div>
    </script>
</head>
<body>
    <header>
        <h2>Facebook API - Get friends activity</h2>
        <a href="https://www.script-tutorials.com/facebook-api-get-friends-activity/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
    </header>
    <img src="facebook.png" class="facebook" alt="facebook" />
    <div id="fb-root"></div>
    <center>
        <h1>Authorization step:</h1>
        <div id="user-info"></div>
        <button id="fb-auth">Please login here</button>
    </center>
    <div id="results"></div>
    <script>
    window.fbAsyncInit = function() {
        FB.init({ appId: '<?= $sApplicationId ?>',
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true
        });
        function updateButton(response) {
            var button = document.getElementById('fb-auth');
            if (response.authResponse) { // in case if we are logged in
                var userInfo = document.getElementById('user-info');
                var iPid = 0;
                FB.api('/me', function(response) {
                    userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
                    button.innerHTML = 'Logout';
                    // get friends activity feed
                    iPid = response.id;
                    if (iPid > 0) {
                        FB.api({ // call fql.query
                            method : 'fql.query',
                            query : "SELECT post_id, actor_id, type, description, permalink, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid = me() AND type = 'newsfeed') AND type != '347'"
                        }, function(response) {
                            // render activity items using 'facebookTemplate' template
                            $('#results').html(
                                $('#facebookTemplate').render(response)
                            );
                        });
                    }
                });
                button.onclick = function() {
                    FB.logout(function(response) {
                        window.location.reload();
                    });
                };
            } else { // otherwise - dispay login button
                button.onclick = function() {
                    FB.login(function(response) {
                        if (response.authResponse) {
                            window.location.reload();
                        }
                    }, {scope:'read_stream'});
                }
            }
        }
        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);
    };
    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>
</body>
</html>

Please pur your application id and secret in the beginning of our script, and only after let’s review our rest code. In the beginning I prepared a small html code:

<div id="fb-root"></div>
<center>
    <h1>Authorization step:</h1>
    <div id="user-info"></div>
    <button id="fb-auth">Please login here</button>
</center>
<div id="results"></div>

We will display short info about logged in member in ‘user-info’ element. Also you can see here the same authorization elements as we made in our previous tutorial (Facebook API – Get friends list). Now, you should pay attention to the JavaScript code of obtaining Facebook friends activity feed:

// get friends activity feed
iPid = response.id;
if (iPid > 0) {
    FB.api({ // call fql.query
        method : 'fql.query',
        query : "SELECT post_id, actor_id, type, description, permalink, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid = me() AND type = 'newsfeed') AND type != '347'"
    }, function(response) {
        // render activity items using 'facebookTemplate' template
        $('#results').html(
            $('#facebookTemplate').render(response)
        );
    });
}

You can ask me why I filtered type #347 – easy, it doesn’t contain anything interesting, this is ‘like’ type. Also, don’t forget, that we use JsRender library again in order to increase the rate of generation of the final HTML code.

Step 2. CSS

In order to stylize our output I used next styles:

#results {
    margin: 0 auto;
    overflow: hidden;
    width: 550px;
}
.fbf {
    background-color: #F7F7F7;
    border: 1px solid #EEEEEE;
    border-radius: 5px 5px 5px 5px;
    clear: left;
    font-size: 1em;
    margin-bottom: 10px;
    overflow: hidden;
    padding: 5px 10px 5px 5px;
}
.fbf img {
    float: left;
    margin-right: 10px;
}
.fbf p {
    margin-bottom: 5px;
    overflow: hidden;
    text-align: left;
}
.fbf a:link, .fbf a:visited, .fbf a:focus, .fbf a:hover, .fbf a:active {
    color: #006699;
    font-size: 12px;
    font-weight: bold;
}
.fbf p span {
    float: right;
    font-size: 11px;
}

Live Demo

[sociallocker]

download in archive

[/sociallocker]


Conclusion

I hope that everything is clean in today’s code. If you have any suggestions about further ideas for articles – you are welcome to share them with us. Good luck in your work!

SIMILAR ARTICLES


NO COMMENTS

Leave a Reply