dongqiao1888 2016-08-05 07:56
浏览 27
已采纳

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!

  • 写回答

1条回答 默认 最新

  • dongyuans61046 2016-08-08 11:29
    关注

    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);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀