PHP Facebook API:一次性获取事件详细信息

时间:2015-02-12 11:19:12

标签: php facebook facebook-graph-api

我想要检索Feed的所有事件并收集事件详细信息。像这样,我正在查询Feed:

    // instantiate
    $facebook = new Facebook($config);

    $pagefeed = $facebook->api("/" . $pageid . "/feed");

    foreach($pagefeed['data'] as $post) {

            // check if post type is a link
            if ($post['type'] == 'event') {
                // call API to retrieve the event details
            }
    }

如您所见,我需要为每个事件发送一个请求。我可以避免使用oData中的“expand”命令吗?

干杯

1 个答案:

答案 0 :(得分:3)

好吧,我自己想通了。到目前为止我使用的方法已经过时了。当使用API​​的版本4x时,它的工作方式如下:

    // init app with app id and secret
    FacebookSession::setDefaultApplication( FB_APP_ID, FB_SECRET);

    // If you're making app-level requests:
    $session = FacebookSession::newAppSession();

    // To validate the session:
    try {
        $session->validate();
    } catch (FacebookRequestException $ex) {
        // Session not valid, Graph API returned an exception with the reason.
    } catch (\Exception $ex) {
        // Graph API returned info, but it may mismatch the current app or have expired.
    }

    try {
        $request = new FacebookRequest(
            $session,
            'GET',
            // get all events of a Page, ?fields= let you choose which fields you want the Graph to return
            '/' . FB_ID . '/events?fields=id,end_time,cover,description,is_date_only,location,name,start_time&since='
                . strtotime('-1 year') . '&until=' . strtotime('+1 year')
                . '&limit=10&locale=de_DE&date_format=d. M, y  H:m'
        );
        $response = $request->execute();
        $graphObject = $response->getGraphObject()->asArray();
        // handle the result -> returns an indexed array without key
        $objectData = $graphObject['data'];

如果你知道怎么做就很简单......; - )

希望这也有助于其他人的争吵。