Facebook PHP SDK和页面Feed帖子(过滤结果)

问题描述:

Facebook PHP SDK和页面供稿发布(过滤结果)

Facebook PHP SDK & Page feed post (filtering results)

问候!我最近一直在研究一种有效的方法来在网站上实施Facebook公共页面供稿(每个帖子都包含描述,日期和图片). 我将以下代码放在一起,使我可以在$ elements上使用foreach.

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 wich 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?

非常感谢!

感谢Luschn和CBroe发表的建议,我一直在阅读/查找并最终使用以下代码.我不确定这是否是正确/最佳的方法,但它似乎确实能很好地工作.

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);

}