[英]Get latest Facebook posts of page with PHP SDK
The following code is inside a file called facebook_posts.php which I call from my index file like so: <?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
以下代码位于名为facebook_posts.php的文件中,我从索引文件中调用它,如下所示: <?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
<?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
. <?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
。 However, where this code is put, there is no response. 但是,在放置此代码的地方,没有响应。 So neither success, nor the catch errors return an error (as I see it). 因此,无论是成功还是捕获错误都不会返回错误(正如我所看到的)。 I tried absolute URLs, but that didn't work either. 我尝试过绝对URL,但这也不起作用。 (I hid the api and page information.) Apparently the content that follows the require_once (footer and scripts) aren't loaded. (我隐藏了api和页面信息。)显然,不会加载require_once(页脚和脚本)后面的内容。 Something seems to go wrong when including the SDK. 包含SDK时似乎出现了问题。
I'm not using composer, do I need to require
the Facebook\\
files or use
them? 我不使用作曲家,我是否require
Facebook\\
文件或use
它们? And which ones do I need for retrieving posts from a page? 从页面检索帖子需要哪些?
<?php
// Defining FB SDK with absolute paths
define('FACEBOOK_SDK_V4_SRC_DIR', 'http://example.com/design/Facebook/');
require 'http://example.com/design/php/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('{my-app-id}','{my-app-secret}');
$session = new FacebookSession('{my-long-lived-access-token}');
// Get the GraphUser object for the current user:
try {
$request = new FacebookRequest(
$session,
'GET',
'/{my-page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
var_dump($graphObject);
echo graphObject;
echo "banana";
} catch (FacebookRequestException $e) {
echo "API ERROR";
} catch (\Exception $e) {
echo "other error";
}
?>
EDIT: so I just required in all the FB files, and that seems to work. 编辑:所以我只需要在所有的FB文件中,这似乎工作。 However, I don't know how to traverse/iterate the object that is returned. 但是,我不知道如何遍历/迭代返回的对象。 Ie how to loop through the different posts (the four latest posts by the page) and echo them in HTML. 即如何循环浏览不同的帖子(页面上的四个最新帖子)并在HTML中回显它们。 A base structure would look like this: 基础结构如下所示:
<time>{publish date}</time>
<p>{post message}</p>
<a href="{link to included url}">{title to included url}</a>
You need to use a long-lived page access-token. 您需要使用长期存在的页面访问令牌。
Page Access Token 页面访问令牌
These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. 这些访问令牌类似于用户访问令牌,除了它们为读取,写入或修改属于Facebook页面的数据的API提供权限。 To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. 要获取页面访问令牌,您需要先获取用户访问令牌并询问manage_pages权限。 Once you have the user access token you then get the page access token via the Graph API. 获得用户访问令牌后,您将通过Graph API获取页面访问令牌。
As @CBroe said, you should not use that access token in client-side code as it is secret/private and you don't want anyone to get it. 正如@CBroe所说,你不应该在客户端代码中使用该访问令牌,因为它是秘密/私有的,你不希望任何人得到它。
So for what you want to do, Javascript is not the right choice. 因此,对于您想要做的事情,Javascript不是正确的选择。 You will have to use some server-side code, like PHP, Python or Ruby to get the posts. 您将不得不使用一些服务器端代码,如PHP,Python或Ruby来获取帖子。 If that is clear, here is how you can create it. 如果这一点很清楚,可以通过以下方式创建它。
Create a Facebook app: 创建一个Facebook应用程序:
The application has disabled OAuth client flow
. 在“高级”设置中,激活OAuth以避免The application has disabled OAuth client flow
。 You need to create a user access token. 您需要创建用户访问令牌。
manage_pages
in the "Extended Permissions" tab. 生成访问令牌:单击“获取访问令牌”并在“扩展权限”选项卡中勾选manage_pages
。 Get your short-lived page access token . 获取您的短期页面访问令牌 。
me/accounts
( GET
), 仍然在Graph API资源管理器中,查询me/accounts
( GET
), Get your long-lived page access token . 获取您的长期页面访问令牌 。
https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3)
in the address bar, 在浏览器中,在地址栏中粘贴https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3)
, access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL
, 从结果中获取新的长期访问令牌: access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL
, Now you can use that new access token to retrieve the posts of your page: 现在,您可以使用该新访问令牌来检索页面的帖子:
$session = new FacebookSession('FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL');
try {
$data = (new FacebookRequest(
$session, 'GET', '/me/posts'
))->execute()->getGraphObject()->getPropertyAsArray("data");
foreach ($data as $post){
$postId = $post->getProperty('id');
$postMessage = $post->getProperty('message');
print "$postId - $postMessage <br />";
}
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}
I've found that the most voted solution is way too complicated and only lasts 2 months. 我发现投票最多的解决方案太复杂了,只持续了2个月。
For me, this is the best solution using an App with Graph API 2.5: 对我来说,这是使用App with Graph API 2.5的最佳解决方案:
1.- Create an App. 1.-创建一个应用程序。
2.- Go to: https://developers.facebook.com/tools/explorer/ 2.-转到: https : //developers.facebook.com/tools/explorer/
3.- Copy this "{ACCESS-TOKEN}" (is in the form: number|hash) 3.-复制此“{ACCESS-TOKEN}”(格式为:number | hash)
IMPORTANT: (This are not app_id|app_secret!!!) 重要提示:(这不是app_id | app_secret !!!)
4.- Query the URL with CURL: 4.-使用CURL查询URL:
(5).- Equivalent URL: (5).-等效URL:
I've put all of this together in a very simple gist: 我把所有这些放在一个非常简单的要点中:
https://gist.github.com/biojazzard/740551af0455c528f8a9 https://gist.github.com/biojazzard/740551af0455c528f8a9
Then code exmaple: 然后代码exmaple:
<ul>
<?php
$page_name = '{PAGE_NAME}'; // Example: http://facebook.com/{PAGE_NAME}
$page_id = '{PAGE_ID}'; // can get form Facebook page settings
$app_id = '{APP_ID}'; // can get form Developer Facebook Page
$app_secret = '{APP_SECRET}'; // can get form Developer Facebook Page
$limit = 5;
function load_face_posts($page_id, $page_name, $app_id, $app_secret, $limit, $message_len) {
$access_token = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials";
$access_token = file_get_contents($access_token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
$access_token = str_replace('access_token=', '', $access_token);
$limit = 5;
$data = file_get_contents("https://graph.facebook.com/$page_name/posts?limit=$limit&access_token=$access_token");
$data = json_decode($data, true);
$posts = $data[data];
//echo sizeof($posts);
for($i=0; $i<sizeof($posts); $i++) {
//echo $posts[$i][id];
$link_id = str_replace($page_id."_", '', $posts[$i][id]);
$message = $posts[$i][message];
echo ($i+1).". <a target='_blank' href='https://www.facebook.com/AqualinkMMC/posts/".$link_id."'>".$message."</a><br>";
}
}
load_face_posts($page_id, $page_name, $app_id, $app_secret, $limit, $message_len);
?>
</ul>
all what you need to get latest posts from facebook feed is described here: http://piotrpasich.com/facebook-fanpage-feed/ 这里描述了从facebook Feed获取最新帖子所需的所有内容: http : //piotrpasich.com/facebook-fanpage-feed/
In shortcut - use your fanpage feed (please replace {id} by proper id) 在快捷方式中 - 使用您的粉丝页面Feed(请使用正确的ID替换{id})
https://facebook.com/feeds/page.php?format=atom10&id= {id} https://facebook.com/feeds/page.php?format=atom10&id= {id}
You can download the feed with this piece of code: 您可以使用以下代码下载Feed:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $rss_url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt($curl, CURLOPT_REFERER, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$raw_xml = curl_exec($curl); // execute the curl command
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.