facebook api_Facebook API –获取朋友活动

facebook api

Facebook API - Get friends activity
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.

Facebook API –今天获得朋友活动我想向您展示如何在您的网站上显示朋友的活动供稿。 有没有想过您的朋友在你们都喜欢的Facebook Pages上做什么? 你们大多数人都可以回答“是”。 今天,我们将在您的网站上显示该活动供稿,我将使用Facebook API(使用fql.query)。 结果脚本可以向我们显示不同的提要类型,例如:状态更新,发布的链接,注释,照片,视频等。 一切都位于“流” FQL表中。 因此,让我们看一下实现。

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

首先,我们应该在Facebook上创建自己的新应用程序。 请点击此链接 ,然后点击左上角的“创建新应用”按钮:

Faceboog Friends API - step 1

Faceboog Friends API-步骤1

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

我们应该选择我们的应用名称,然后单击“继续”按钮。 我们只有自己的api密钥:

Faceboog Friends API - step 2

Faceboog Friends API-步骤2

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

现在,请注意“基本信息”块,我们应该输入“应用程序域”和“网站URL”参数:

Faceboog Friends API - step 3

Faceboog Friends API-步骤3

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

就是这样,单击“保存更改”按钮,然后开始编码!

现场演示

步骤1. PHP (Step 1. PHP)

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

现在,请创建一个空的index.php文件并放入下一个代码:

index.php (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>

<?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:

请在我们的脚本开头(只有在我们回顾完其余代码之后),才能提供您的应用程序ID和密码。 在开始时,我准备了一个小的html代码:


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

<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:

我们将在“ user-info”元素中显示有关已登录成员的简短信息。 您还可以在这里看到与上一教程( Facebook API –成为朋友列表 )相同的授权元素。 现在,您应该注意获取Facebook朋友活动供稿JavaScript代码:


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

// 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.

您可以问我为什么过滤了#347型-简单,它不包含任何有趣的东西,这是'like'类型。 另外,不要忘记,我们再次使用JsRender库是为了提高最终HTML代码的生成速度。

步骤2. CSS (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;
}

#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;
}

现场演示

[sociallocker]

[社交储物柜]

存档下载

[/sociallocker]

[/ 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!

我希望今天的代码中的所有内容都是干净的。 如果您对文章的进一步建议有任何建议,欢迎与我们分享。 祝您工作顺利!

翻译自: https://www.script-tutorials.com/facebook-api-get-friends-activity/

facebook api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值