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

问题描述:

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

Thank you very much!

Facebook PHP SDK& 页面提要帖子(过滤结果) p>

问候! 我最近一直致力于在网站上实施Facebook公共页面提要(每个帖子包含描述,日期和图片)的有效方法。 我把以下代码放在一起,允许我在$ elements上使用foreach。 p>

  $ pageid ='#PIDID#'; 
 $ accesstoken ='  #ACCESSTOKEN#'; 
 $ url =“https://graph.facebook.com/v2.7/$pageid/feed?limit=20&access_token=$accesstoken";
nnfunction getfb($ url){  
 
 $ curl = curl_init(); 
 curl_setopt($ curl,CURLOPT_URL,$ url); 
 curl_setopt($ curl,CURLOPT_REFERER,''); 
 curl_setopt($ curl,CURLOPT_FOLLOWLOCATION,1); \  n curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1); 
 curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,false); 
 curl_setopt($ curl,CURLOPT_TIMEOUT,10); 
 $ raw_xml = curl_exec($ curl);  //执行curl命令
 $ result = json_decode($ raw_xml,true); 
 
返回$ result; 
} 
 
 $ elements = getfb($ url); 
 nforeach($ elements [' 数据']为$ 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);  
  code>  pre> 
 
 

它确实很有效,但不幸的是,它不是仅列出所有者在页面上发布的帖子,而是列出了“访客帖子”框内发布的帖子 ...我不想要。 你知道吗和/或能帮我弄清楚如何过滤这些结果,只列出页面所有者发布的帖子? p>

非常感谢你! p> div>

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

}