我刚刚开始涉足JSON和Facebook的Graph API。通过以下内容,我可以从我的Facebook页面中提取帖子:
$page_id = '???';
$access_token = '???';
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?access_token=' . $access_token . '&limit=100');
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post ) {
$posts .= '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="' . $post->link . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}
echo $posts;
然而Facebook在一个JSON请求中不允许超过100个帖子。有没有办法通过提出多个请求或者我是否应该以完全不同的方式解决这个问题?
有人知道如何显示我的Facebook页面上的所有现有帖子吗?
答案 0 :(得分:1)
可以通过对它们进行分页来显示无限量的帖子:
//Set page variable/page number
if($_GET['page'] == 0) {
$page = 1;
}
else {
$page = $_GET['page'];
}
//Page increments/decrements
$next_page = intval($page + 1);
$prev_page = intval($page - 1);
//Set offset if it isn't the first page
if ($page > 1) {
$offset = $page * 5 - 5;
}
//Facebook Dev details
$page_id = '???';
$access_token = '???';
//Get JSON for specified page
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?access_token=' . $access_token . '&limit=5' . '&offset=' . $offset);
//Interpret the data
$fb_data = json_decode($json_object);
foreach ($fb_data->data as $post ) {
$posts .= '<p><a href="http://facebook.com/' . $post->id . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="http://facebook.com/' . $post->id . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}
//Display posts
echo $posts;
//If page isn't the first page add previous button
if ($page > 1) {
echo '<a href="?page=' . $prev_page . '">' . 'Previous' . '</a>';
}
//Total offset of posts on next page
$next_page_offset = $offset + 5;
//Make next page object
$json_next_page_object = @file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?access_token=' . $access_token . '&limit=5' . '&offset=' . $next_page_offset);
//Get JSON for next page
$fb_next_data = json_decode($json_next_page_object);
//Echo next button if size of next data is greater than one
if( sizeof( $fb_next_data->data) > 0 ) {
echo '<a href="?page=' . $next_page . '">' . 'Next' . '</a>';
}
可能需要一些改进,但到目前为止它仍能正常运作。
我希望我到目前为止能够帮助其他人制作基本且易于管理的Facebook Feed:]