Get latest Facebook posts of page with PHP SDK(使用 PHP SDK 获取页面的最新 Facebook 帖子)
问题描述
以下代码位于一个名为 facebook_posts.php 的文件中,我从索引文件中调用该文件,如下所示:<?php require_once("http://www.example.com/design/php/facebook_posts.php");?>代码>.但是,放置此代码的位置没有响应.因此,成功和 catch 错误都不会返回错误(如我所见).我尝试了绝对 URL,但这也不起作用.(我隐藏了 api 和页面信息.)显然,require_once 后面的内容(页脚和脚本)没有加载.包含 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"); ?>
. 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. (I hid the api and page information.) Apparently the content that follows the require_once (footer and scripts) aren't loaded. Something seems to go wrong when including the SDK.
我没有使用 composer,我需要 require
Facebook
文件还是 use
它们?从页面中检索帖子需要哪些?
I'm not using composer, do I need to require
the Facebook
files or use
them? 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 FacebookFacebookSession;
use FacebookFacebookRequest;
use FacebookGraphUser;
use FacebookFacebookRequestException;
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";
}
?>
所以我只是 所有 FB 文件都需要,这似乎可行.但是,我不知道如何遍历/迭代返回的对象.IE.如何循环浏览不同的帖子(页面的四个最新帖子)并在 HTML 中回显它们.基本结构如下所示:
so I just required in all the FB files, and that seems to work. However, I don't know how to traverse/iterate the object that is returned. I.e. how to loop through the different posts (the four latest posts by the page) and echo them in 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.
这些访问令牌类似于用户访问令牌,除了它们向 API 提供读、写权限或修改属于 Facebook 主页的数据.获取页面访问令牌,您需要首先获取用户访问令牌和请求 manage_pages 权限.一旦您拥有用户访问权限令牌,然后您可以通过 Graph API 获取页面访问令牌.
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. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
正如@CBroe 所说,您不应在客户端代码中使用该访问令牌,因为它是秘密/私有的,您不希望任何人获得它.
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.
因此,对于您想做的事情,Javascript 不是正确的选择.您将不得不使用一些服务器端代码,如 PHP、Python 或 Ruby 来获取帖子.如果这很清楚,那么您可以通过以下方式创建它.
So for what you want to do, Javascript is not the right choice. You will have to use some server-side code, like PHP, Python or Ruby to get the posts. If that is clear, here is how you can create it.
创建 Facebook 应用:
Create a Facebook app:
- 将应用 ID (1) 和应用机密 (2) 放在一边,
- 在高级"设置中,激活 OAuth 以避免
应用程序已禁用 OAuth 客户端流程
.
- keep the app id (1) and app secret (2) aside,
- in the "advanced" settings, activate OAuth in order to avoid
The application has disabled OAuth client flow
.
您需要创建一个用户访问令牌.
You need to create a user access token.
- 继续 Graph API Explorer 并选择您刚刚创建的应用程序,
- 生成访问令牌:点击获取访问令牌"并在扩展权限"选项卡中勾选
manage_pages
.
- go on the Graph API Explorer and select the application you just created,
- generate an access token: click "Get access token" and tick
manage_pages
in the "Extended Permissions" tab.
获取您的短期页面访问令牌.
- 仍在 Graph API 资源管理器中,查询
me/accounts
(GET
), - 找到您的页面并获取其访问令牌(3).
获取您的长期页面访问令牌.
- 在您的浏览器中,粘贴
https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3)代码>在地址栏中,
- 将 (1)、(2) 和 (3) 替换为您的应用 ID、应用密码和页面访问令牌,
- 从结果中获取新的长期访问令牌:
access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzu
- 使用访问令牌调试器来验证您的访问令牌将永不过期.
- in your browser, paste
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, - replace (1), (2) and (3) by your app id, your app secret and your page access token,
- get your new long-lived access token from the result:
access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL
, - use the Access Token Debugger to verify that your access token will never expire.
<小时>
现在您可以使用新的访问令牌来检索您页面的帖子:
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
}
这篇关于使用 PHP SDK 获取页面的最新 Facebook 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP SDK 获取页面的最新 Facebook 帖子
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Laravel 仓库 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01