Facebook PHP SDK & Page feed post (filtering results)
Greetings! I've been working lately on an effective way to implement a Facebook public page feed (each post featuring description, date and picture) on a website. I've put put together the following code which allows me to use a foreach on $elements.
$pageid = '#PAGEID#';
$accesstoken = '#ACCESSTOKEN#';
$url = "https://graph.facebook.com/v2.7/$pageid/feed?limit=20&access_token=$accesstoken";
function getfb($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
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
$result = json_decode($raw_xml, true);
return $result;
}
$elements = getfb($url);
foreach($elements['data'] as $k => $v){
$url = "https://graph.facebook.com/v2.7/{$v['id']}?fields=full_picture,picture&access_token=$accesstoken";
$fields = getfb($url);
$elements['data'][$k]['pictures'] = $fields;
}
var_dump($elements);
It does work nicely, but unfortunately instead of listing only the posts published on the page by the owner, it also lists posts published inside the box "Visitor Posts"... which I do not want. Do you know and/or can help me figure out how to filter those results in such a way to only list posts published by page owner?
Thank you very much!
Thanks to the suggestions Luschn and CBroe posted I kept on reading/looking and endend up using the following code. I'm not sure yet if this is the correct/best way of doing so, but it does seem to working nicely.
require_once ('facebook/autoload.php'); // See https://developers.facebook.com/docs/reference/php/
$facebook_page_id = 'xxx';
$facebook_app_secret = 'yyy';
$facebook_app_id = 'zzz';
$facebook_graph_version = 'v2.6';
$fb = new Facebook\Facebook([
'app_id' => $facebook_app_id,
'app_secret' => $facebook_app_secret,
'default_graph_version' => $facebook_graph_version
]);
$response = $fb->get( '/'.$facebook_page_id.'/posts?fields=message,full_picture,link,updated_time,picture&limit=5', $fb->getApp()->getAccessToken() );
$get_data = $response->getDecodedBody(); // for Array resonse
foreach ( $get_data['data'] as $single ) {
var_dump($single);
}