2016-07-05 31 views
0

我剛開始涉足JSON和Facebook的Graph API。用下面的我已經能夠從我的Facebook頁面拉帖子:Facebook圖形API:只能顯示100個帖子

$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

您遇到的錯誤是什麼?沒有這一點,我們不能幫助。 –

+0

@BRO_THOM對不起,我應該更清楚地解釋。據我所知,Facebook只允許你在一個JSON請求中提取100個帖子。我需要找到一種方法來發出多個請求,直到顯示所有帖子,或者我需要以完全不同的方式進行討論。 –

+0

然後我看到你的問題。您應該在達到100個帖子的末尾時觸發ajax或pjax調用,以獲得另外100個帖子。有關更多信息,請參閱http://api.jquery.com/jquery.ajax/。 –

回答

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訂閱源:]

相關問題