php 在网站上显示 Facebook 新闻源/时间线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10208754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying a Facebook newsfeed/timeline on a website
提问by Chuck Le Butt
I have a client who wants their company's Facebook newsfeed/timeline to be displayed on their website. It's not a personal timeline/newsfeed, but an organisation's.
我有一个客户希望他们公司的 Facebook 新闻源/时间线显示在他们的网站上。这不是个人时间表/新闻源,而是组织的。
Everything I've read seems a few years old, but the upshot appears to be: Facebook wants to keep all its data on its own servers -- they don't want people exporting it, and people have been banned for trying. (As I say, this information was several years old.)
我读过的所有内容似乎都有几年的历史,但结果似乎是:Facebook 希望将其所有数据保存在自己的服务器上——他们不希望人们将其导出,而且人们因尝试而被禁止。(正如我所说,这些信息已经有好几年了。)
The closest current thing I've found is the Activity Feed Plugin, but that only registers other user's interactions with the site or a FB app.
我发现的最近的东西是Activity Feed Plugin,但它只注册其他用户与网站或 FB 应用程序的交互。
Has anyone had any success exporting their public updates to an external website, or do I have to tell my client that it can't be done?
有没有人成功地将他们的公开更新导出到外部网站,或者我必须告诉我的客户这是不可能的吗?
Thanks for any help!
谢谢你的帮助!
回答by bool.dev
AFAIK, it is possible, in a way. The simplest solution, but not best for your situation could be the Like Box plugin:
AFAIK,在某种程度上是可能的。最简单但不适合您情况的解决方案可能是Like Box 插件:
The Like Box enables users to:
See how many users already like this Page, and which of their friends like it too
Read recent postsfrom the Page
Like the Page with one click, without needing to visit the Page
Like Box 使用户能够:
查看已有多少用户喜欢此主页,以及他们的哪些朋友也喜欢它 一键
阅读主页上的 最新帖子
喜欢主页,无需访问主页
Better solution: use their Graph API, however you can only read the data(as JSON), not have the stream exactly replicated on your client's website, don't expect to be able to apply the styles that facebook uses(i.e you won't be able to scrape it), you'll have to either replicate it, or create your own styles.
更好的解决方案:使用他们的Graph API,但是您只能读取数据(作为 JSON),不能在您客户的网站上准确复制流,不要期望能够应用 facebook 使用的样式(即您不会t 能够刮它),您必须复制它或创建自己的样式。
Now if the page is public and can be read by all, as in there are no privacy rules, then you can simply call the url with any valid access_token(can be app access_token also):
现在,如果页面是公开的并且所有人都可以阅读,因为没有隐私规则,那么您可以简单地使用任何有效的 access_token(也可以是 app access_token)调用该 url:
https://graph.facebook.com/<clientpagename_OR_id>/feed
or
或者
https://graph.facebook.com/<clientpagename_OR_id>/posts
depending on what exactly you need, try the graph api explorerto check that(and also see the kind of data being returned). When there are lots of posts, there will be pagination urls, which you'll be able to notice in the explorer too.
根据您的需要,尝试使用图形 API 资源管理器进行检查(并查看返回的数据类型)。当有很多帖子时,会有分页网址,您也可以在资源管理器中注意到。
Incase the page is not public, you'll need an access_token with the read_stream permission, hence you'll need to create a facebook app, of type website. Then get your client's page's admin to authorize the app, with read_streampermission. After that you can call the urls with the access_token that you receive after authentication and can continue reading the stream.
如果页面不是公开的,您需要一个具有 read_stream 权限的 access_token,因此您需要创建一个facebook 应用程序,类型为网站。然后让您的客户页面的管理员授权该应用程序,并read_stream获得许可。之后,您可以使用身份验证后收到的 access_token 调用 url,并可以继续读取流。
https://graph.facebook.com/<clientpagename_OR_id>/posts?access_token=thetoken
In this case use the PHP SDK, to simplify authentication and calling the graph api.
在这种情况下,使用PHP SDK来简化身份验证和调用图形 API。
Important Links: Authentication Guide, Real-time-updates.
Good luck.
祝你好运。
Edit: you do need an access token to access the feed or posts connections, but you donot necessarily need an access token to read the page object itself, as given in this documentation.
Note from the doc:
编辑:您确实需要访问令牌来访问提要或帖子连接,但您不一定需要访问令牌来读取页面对象本身,如本文档中所述。
文档中的注释:
For connections that require an access token, you can use any valid access token if the page is public and not restricted. Connections on restricted pages require a user access token and are only visible to users who meet the restriction criteria (e.g. age) set on the page.
对于需要访问令牌的连接,如果页面是公开的且不受限制,您可以使用任何有效的访问令牌。受限页面上的连接需要用户访问令牌,并且仅对满足页面上设置的限制条件(例如年龄)的用户可见。
回答by Gunnar Karlsson
You can retrieve the organization's newsfeed using Facebook's Graph API. Timeline can't be retrieved via public API.
您可以使用 Facebook 的 Graph API 检索组织的新闻源。无法通过公共 API 检索时间轴。
There is no plugin to do this. You would need to call
没有插件可以做到这一点。你需要打电话
https://graph.facebook.com/USER_ID/home
which gives you a JSON response.
这会给你一个 JSON 响应。
You then need to parse the JSON into a new layout on the organization's web page.
然后,您需要将 JSON 解析为组织网页上的新布局。
Confusingly, calling
令人困惑的是,调用
https://graph.facebook.com/USER_ID/feed
doesn't retrieve the newsfeed, but a user's wall posts, which may or may not be what you want.
不检索新闻提要,而是检索用户的墙贴,这可能是您想要的,也可能不是。
Hereis a tutorial that goes through the basics of setting up a newsfeed on a website with php.
这是一个教程,介绍了使用 php 在网站上设置新闻源的基础知识。
回答by Jan-Paul Kleemans
The easiest way to do this is to read the Facebook timeline RSS:
最简单的方法是阅读 Facebook 时间线 RSS:
function FacebookFeed($pagename, $count, $postlength) {
$pageID = file_get_contents('https://graph.facebook.com/?ids='.$pagename.'&fields=id');
$pageID = json_decode($pageID,true);
$pageID = $pageID[$pagename]['id'];
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
$rssUrl = 'http://www.facebook.com/feeds/page.php?format=rss20&id='.$pageID;
$xml = simplexml_load_file($rssUrl);
$entry = $xml->channel->item;
for ($i = 0; $i < $count; $i++) {
$description_original = $entry[$i]->description;
$description_striphtml = strip_tags($description_original);
$description = substr($description_striphtml, 0, $postlength);
$link = $entry[$i]->link;
$date_original = $entry[$i]->pubDate;
$date = date('d-m-Y, H:i', strtotime($date_original));
$FB_feed .= $description."…<br>";
$FB_feed .= "<small><a href='".$link."'>".$date."</a></small><br><br>";
}
return $FB_feed;
}
回答by Newton Joshua
This document details the steps to obtain the facebook access tokens and the using the tokens to fetch FB feeds.
本文档详细介绍了获取 facebook 访问令牌和使用令牌获取 FB 提要的步骤。
Example:A live example is available in
示例:一个活生生的例子可在
Introduction to Graph API:The Graph API is the primary way to get data in and out of Facebook's platform. It's a low-level HTTP-based API that you can use to query data, post new stories, manage ads, upload photos and a variety of other tasks that an app might need to do.
Graph API 简介:Graph API 是将数据传入和传出 Facebook 平台的主要方式。这是一个基于 HTTP 的低级 API,您可以使用它来查询数据、发布新故事、管理广告、上传照片以及应用程序可能需要执行的各种其他任务。
FaceBook Apps:
脸书应用程序:
Create a Facebook app. You will get an App_Idand App_Secret
创建一个 Facebook 应用程序。你会得到一个App_Id和App_Secret
Graph API Explorer:
图 API 资源管理器:
https://developers.facebook.com/tools/explorer/
{{App_Id}}/?method=GET&path=me%2Ffeed&version=v2.8
https://developers.facebook.com/tools/explorer/
{{App_Id}}/?method=GET&path=me%2Ffeed&version=v2.8
You will get an access_tokenwhich is short lived. So this will be our short_lived_access_token.
你会得到一个access_token短暂的。所以这将是我们的short_lived_access_token.
note: while creating access token select all the fb fields that you require.This will give permission to the access token to fetch those fields.
注意:在创建访问令牌时,选择您需要的所有 fb 字段。这将授予访问令牌以获取这些字段的权限。
Access Token Extension:
访问令牌扩展:
https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={{App_Id}}&client_secret={{App_Secret}}&fb_exchange_token={{short-lived-access_token}}
https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id= {{App_Id}}&client_secret= {{App_Secret}}&fb_exchange_token= {{short-lived-access_token}}
You will get an access_tokenwith a validity of 2 months.
您将获得access_token有效期为 2 个月的证书。
Access Token Debugger:
访问令牌调试器:
https://developers.facebook.com/tools/debug/accesstoken?q={{access_token}}&version=v2.8
https://developers.facebook.com/tools/debug/accesstoken?q= {{access_token}}&version=v2.8
you can check check the details of the access_token.
您可以检查检查的详细信息access_token。
Facebook SDK for JavaScript:Include the below JavaScript in your HTML to asynchronously load the SDK into your page
适用于 JavaScript 的 Facebook SDK:在您的 HTML 中包含以下 JavaScript 以将 SDK 异步加载到您的页面中
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
Graph API:Let's make an API call to get our FB id, profile pic, cover pic and feeds.
Graph API:让我们调用 API 来获取我们的 FB id、个人资料图片、封面图片和提要。
window.fbAsyncInit = function () {
FB.init({
appId: '{{App_Id }}',
xfbml: true,
version: 'v2.7'
});
FB.api(
'/me',
'GET', {
fields: 'id,picture{url},cover,feed',
access_token: {{access_token}}
},
function (response) {
if (response.error) {
console.error(response.error.message);
}
if (response.picture.data.url) {
profilePic = response.picture.data.url;
}
if (response.cover.source) {
coverPic = response.cover.source;
}
if (response.feed.data) {
feeds = response.feed.data;
feeds.forEach(function (feed) {
// view each feed content
});
}
if (response.feed.paging.next) {
nextFeedPage = response.feed.paging.next;
// a request to nextFeedPage will give the next set of feeds
}
}
);
};
Use the Graph API Explorer to design your query that should be entered in the 'fields' (eg: 'id,picture{url},cover,feed')
使用 Graph API Explorer 设计应在“字段”中输入的查询(例如:“id,picture{url},cover,feed”)
Now you can fetch your Facebook data from Facebook Graph API using your access_token.
现在您可以使用您的 access_token 从 Facebook Graph API 获取您的 Facebook 数据。
Refer to https://developers.facebook.com/docs/graph-api/overview/
参考https://developers.facebook.com/docs/graph-api/overview/
Note: Your access_tokenwill expire in 2 months. Create a new access_token after that.
注意:您的access_token有效期为 2 个月。之后创建一个新的 access_token。
回答by John Brackett
It can definitely be done. You just need to get an access token through facebook and then you can access a JSON feed of posts through the facebook API.
绝对可以做到。您只需要通过 facebook 获取访问令牌,然后您就可以通过 facebook API 访问帖子的 JSON 提要。
You need to go to the facebook developer site and click on Apps at the top. Follow the steps to get an app secret and client ID. Then just put them into the following URL and it will return your access token:
您需要转到 facebook 开发者网站,然后单击顶部的应用程序。按照步骤获取应用程序密钥和客户端 ID。然后只需将它们放入以下 URL,它就会返回您的访问令牌:
Step by step instructions here: http://smashballoon.com/custom-facebook-feed/access-token/
这里的分步说明:http: //smashballoon.com/custom-facebook-feed/access-token/
回答by Amit
Yes, it can be done. First register the web site at facebook's developer page. Than you can use any suitable API for interacting with FB. Sometimes ago I used SpringSocial (since I was working tightly with Spring)... You can use FB's own api which is also very useful you can read the tutorial here
是的,这是可以做到的。首先在 facebook 的开发者页面注册网站。您可以使用任何合适的 API 与 FB 进行交互。以前有时我使用 SpringSocial(因为我与 Spring 紧密合作)...您可以使用 FB 自己的 api,这也非常有用,您可以在此处阅读教程

