I am using PHP SDK 5.0. I have been able to pull posts from my FB page and display it on my site with this code below. I need help to achieve pagination of the results returned by this code.
session_start();
require_once __DIR__. '/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxx',
'default_graph_version' => 'v2.4',
'default_access_token' => 'xxxxxxxxxxxx',
]);
$request = $fb->request('GET','/500px/feed/', array('fields' => 'created_time,message', 'limit' => '3',));
try {
$response = $fb->getClient()->sendRequest($request);
$data_array = $response->getDecodedBody();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
Facebook returns the next and previous links with every feed-result, like in the example below. How can I get pagination using those ? Or is there a better alternative.
[data] => Array
(
[0] => Array
(
[created_time] => 2015-09-23T17:00:53+0000
[message] => some message pulled from the post
[id] => 57284451149_10152992926641150
)
)
[paging] => Array
(
[previous] => https://graph.facebook.com/v2.4/57284451149/feed?fields=created_time,message&limit=1&since=1443027653&access_token=xxxxxxxxxxx&__paging_token=enc_xxxxxxxxxxxx&__previous=1
[next] => https://graph.facebook.com/v2.4/57284451149/feed?fields=created_time,message&limit=1&access_token=xxxxxxxxxxx&until=1443027653&__paging_token=enc_xxxxxxxxxxxx
)
I am still learning PHP, and at this point I have no clue how to go beyond this. Ideally there would be three results per page and the results would display on the same page. If not a solution, I would really appreciate pseudo codes or helpful suggestions or a roadmap which would help me do it myself.
TEMP SOLUTION - I could be on wrong track, but here is what I have done as a temporary solution. Seems like facebook does all the work, eg offset etc and provides us a calculated url, and all we need to do is work with the provided urls.
//set your url parameters here
function fetchUrl($url){
if(is_callable('curl_init')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$feedData = curl_exec($ch);
curl_close($ch);
}
return $feedData;
}
if(isset($_POST['nextLink'])){
$url = $_POST['nextLink'];
} elseif(isset($_POST['prevLink'])){
$url = $_POST['prevLink'];
} else {
$url = 'https://graph.facebook.com/v2.4/'.$pageID.'/feed?fields='.$fields.'&limit='.$limit.'&access_token='.$accessToken;
}
$json_object = fetchUrl($url);
$FBdata = json_decode($json_object, true);
//parse the received data in your desired format
//display data
//get the next link, construct and set a url
$nextLink = $FBdata['paging']['next'];
$prevLink = $FBdata['paging']['previous'];
//Your next and previous form here
I would use http GET
method but i don't prefer ugly long urls so I am using POST
method to get next
and previous
urls. Note that I using cURL instead of PHP SDK. This is a simplified example and needs more work.
I am not writing this as an answer because this is just a woraround not a solution, I am still looking to do it using PHP SDK. I just could not gt hold of SDK
generated url. Any inputs ?