下面的代码在一个名为facebook_posts.php的文件中,我从索引文件中这样调用它:<?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
。但是,在放置此代码的地方,没有任何响应。因此,无论是成功还是捕获错误都不会返回错误(在我看来(。我尝试了绝对URL,但也不起作用。(我隐藏了api和页面信息。(显然,require_one(页脚和脚本(后面的内容没有加载。在包含SDK时似乎出现了问题。
我没有使用composer,我需要require
Facebook'
文件还是use
文件?从页面中检索帖子需要哪些?
<?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";
}
?>
编辑:所以我只是要求在所有的FB文件,这似乎工作。但是,我不知道如何遍历/迭代返回的对象。即,如何循环浏览不同的帖子(页面上最新的四篇帖子(并在HTML中进行回显。基本结构如下所示:
<time>{publish date}</time>
<p>{post message}</p>
<a href="{link to included url}">{title to included url}</a>
您需要使用长期页面访问令牌。
页面访问令牌
这些访问令牌类似于用户访问令牌,除了它们为读、写API提供权限或者修改属于Facebook页面的数据。获取页面访问令牌,您需要通过获取用户访问令牌和请求manage_pages权限。一旦您拥有用户访问权限令牌,然后通过Graph API获取页面访问令牌。
正如@CBroe所说,你不应该在客户端代码中使用该访问令牌,因为它是秘密/私有的,你不希望任何人得到它。
因此,对于您想要做的事情,Javascript不是正确的选择。您将不得不使用一些服务器端代码,如PHP、Python或Ruby来获取帖子。如果这是明确的,下面是如何创建它
-
创建一个Facebook应用程序:
- 保留应用程序id(1(和应用程序机密
- 在"高级"设置中,激活OAuth以避免
The application has disabled OAuth client flow
-
您需要创建一个用户访问令牌。
- 在Graph API资源管理器上,选择您刚刚创建的应用程序
- 生成访问令牌:单击"获取访问令牌"并在"扩展权限"选项卡中勾选
manage_pages
-
获取您短暂的页面访问令牌。
- 仍然在图形API资源管理器中查询
me/accounts
(GET
( - 找到您的页面并获取其访问令牌(3(
- 仍然在图形API资源管理器中查询
-
获取您的长期页面访问令牌。
- 在浏览器中,将
https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3)
粘贴到地址栏中 - 用您的应用id、应用密钥和页面访问令牌替换(1(、
- 从结果中获取新的长期访问令牌:CCD_ 10
- 使用访问令牌调试器验证您的访问令牌是否永远不会过期
- 在浏览器中,将
现在你可以使用这个新的访问令牌来检索你页面的帖子:
$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
}
我发现投票最多的解决方案太复杂了,只能持续2个月。
对我来说,这是使用Graph API 2.5:的应用程序的最佳解决方案
1.-创建一个应用程序。
2.-转到:https://developers.facebook.com/tools/explorer/
- 在右上角选择新创建的应用程序
- 选择"获取应用程序令牌">
3.-复制此"{ACCESS-TOKEN}"(形式为:数字|哈希(
重要提示:(这不是app_id|app_secret!!(
4.-使用CURL:查询URL
- https://graph.facebook.com/{PAGE-ID}/帖子?access_token={access-token}
(5( .-等效URL:
- https://graph.facebook.com/{PAGE-ID}/提要?access_token={access-token}
我把所有这些放在一起,用一个非常简单的要点:
https://gist.github.com/biojazzard/740551af0455c528f8a9
- 创建应用程序开发人员Facebook页面
然后代码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>
我的网站上介绍了从facebook订阅源获取最新帖子所需的所有内容:http://piotrpasich.com/facebook-fanpage-feed/
在快捷方式中-使用您的粉丝页面订阅源(请用正确的id替换{id}(
https://facebook.com/feeds/page.php?format=atom10&id={id}
您可以使用以下代码下载提要:
$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