php 如何将我的 facebook 页面中最近的最新帖子显示到我的网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10514497/
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
how to display latest recent posts in my facebook page to my website
提问by user1080247
i have page on Facebook and I want to display latest 5 posts from my feed/wall on a page to my website. How to do this? I found this solution.. it is easy
我在 Facebook 上有页面,我想在我网站的页面上显示来自我的提要/墙上的最新 5 个帖子。这该怎么做?我找到了这个解决方案..很容易
https://developers.facebook.com/docs/reference/plugins/like-box/
and someone guide me to use facebook apiand do it myself what is the best way?
有人指导我使用facebook api并自己做最好的方法是什么?
I use php mysql to build this site
我使用 php mysql 来建立这个网站
回答by Okky
Here is the PHP code. You need to place this in your template.
这是PHP代码。你需要把它放在你的模板中。
<ul>
<?php
//function to retrieve posts from facebook's server
function loadFB($fbID){
$url = "http://graph.facebook.com/".$fbID."/feed?limit=3";
// Update by MC Vooges 11jun 2014: Access token is now required:
$url.= '&access_token=YOUR_TOKEN|YOUR_ACCESS_SECRET';// *
//load and setup CURL
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//get data from facebook and decode JSON
$page = json_decode(curl_exec($c));
//close the connection
curl_close($c);
//return the data as an object
return $page->data;
}
/* Change These Values */
// Your Facebook ID
$fbid = "190506416472588";
// How many posts to show?
$fbLimit = 10;
// Your Timezone
date_default_timezone_set("America/Chicago");
/* Dont Change */
// Variable used to count how many we've loaded
$fbCount = 0;
// Call the function and get the posts from facebook
$myPosts = loadFB($fbid);
//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
//only show posts that are posted by the page admin
if($dPost->from->id==$fbid){
//get the post date / time and convert to unix time
$dTime = strtotime($dPost->created_time);
//format the date / time into something human readable
//if you want it formatted differently look up the php date function
$myTime=date("M d Y h:ia",$dTime);
?>
<ul>
<li><?php echo($dPost->message) . $myTime; ?></li>
</ul>
<?php
//increment counter
$fbCount++;
//if we've outputted the number set above in fblimit we're done
if($fbCount >= $fbLimit) break;
}
}
?>
</ul>
Two things you must do for working out this script.
编写此脚本必须做两件事。
Make sure your server is cURL enabled
You will have change the Facebook ID in the script by yours.
确保您的服务器启用了 cURL
您将更改脚本中的 Facebook ID。
* You can get the access token this way:
* 您可以通过以下方式获取访问令牌:
$token = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials';
$token = file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
回答by Deele
- Login to facebook
- Go to facebok developers section "Apps"
- Register new app, you need only to register new app, all additional data is optional
- Copy your App ID/API Key and App Secret from that same "Apps" section.
- Copy facebook.phpand base_facebook.phpfiles from repoto your server
Use polymorphic queryto api, to request wall content from facebook account
require 'facebook.php'; $facebook = new Facebook(array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET', )); $fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5'); if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) { // display contents of $fbApiGetPosts["data"] array }Replace YOUR_APP_ID with your app ID, YOUR_APP_SECRET with your app secret and YOUR_FACEBOOK_ACCOUNT_ID with target facebook account, you want to get posts from.
- 登录脸书
- 转到 Facebook 开发者部分“应用程序”
- 注册新应用程序,您只需要注册新应用程序,所有附加数据都是可选的
- 从同一“应用程序”部分复制您的应用程序 ID/API 密钥和应用程序密钥。
- 将facebook.php和base_facebook.php文件从repo复制到您的服务器
对api使用多态查询,从facebook账户请求墙内容
require 'facebook.php'; $facebook = new Facebook(array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET', )); $fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5'); if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) { // display contents of $fbApiGetPosts["data"] array }将 YOUR_APP_ID 替换为您的应用 ID,将 YOUR_APP_SECRET 替换为您的应用机密,将 YOUR_FACEBOOK_ACCOUNT_ID 替换为您希望从中获取帖子的目标 Facebook 帐户。
Polymorphic query basically is path/URL. More info inside previously mentioned facebook api reference docs.
多态查询基本上是路径/URL。前面提到的 facebook api参考文档中的更多信息。
If your target facebook account wall is public, you won't need anything else that this, to view them.
如果您的目标 facebook 帐户墙是公开的,您将不需要任何其他内容来查看它们。
回答by DACrosby
I had trouble with Okky's answer here, and I found a possible, albeit not ideal work around.
我在这里对 Okky 的回答遇到了麻烦,我找到了一个可能的,尽管不是理想的解决方法。
Use an RSS feed of your Facebook wall, then simply parse it with an RSS reader of your choosing.
使用 Facebook 墙的 RSS 提要,然后使用您选择的 RSS 阅读器简单地解析它。
https://www.facebook.com/feeds/page.php?format=rss20&id=YOUR_UNIQUE_ID
https://www.facebook.com/feeds/page.php?format=rss20&id=YOUR_UNIQUE_ID
回答by Fabien TheSolution
So to mix up Okky and Deele answer, that both help me out, you must end with something that will look like this. I also add an anchor tag to link to the post url :
因此,要将 Okky 和 Deele 的答案(两者都帮助我)混合起来,您必须以看起来像这样的内容结束。我还添加了一个锚标签来链接到帖子网址:
<?php
$fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5');
if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
//loop through all the posts we got from facebook
foreach($fbApiGetPosts["data"] as $dPost){
//only show posts that are posted by the page admin
if($dPost["from"]["id"]==$fbid){
//get the post date / time and convert to unix time
$dTime = strtotime($dPost["created_time"]);
//format the date / time into something human readable
//if you want it formatted differently look up the php date function
$myTime=date("M d Y h:ia",$dTime);
?>
<li><a href="<?php echo($dPost["link"]); ?>">
<?php echo($dPost["message"]) . "<br>" .
$myTime; ?></a></li>
<?php
}
}
}
?>

