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

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像