You are here

public function FacebookPostCollector::getPosts in Social Feed 8

Fetch Facebook posts from a given feed.

Parameters

string $page_name: The name of the page to fetch results from.

string $post_types: The post types to filter for.

int $num_posts: The number of posts to return.

Return value

array An array of Facebook posts.

Throws

\Facebook\Exceptions\FacebookSDKException

File

src/Services/FacebookPostCollector.php, line 119

Class

FacebookPostCollector
Class FacebookPostCollector.

Namespace

Drupal\socialfeed\Services

Code

public function getPosts($page_name, $post_types, $num_posts = 10) {
  $posts = [];
  $post_count = 0;
  $url = $page_name . $this
    ->getFacebookFeedUrl($num_posts);
  do {
    $response = $this->facebook
      ->get($url);

    // Ensure not caught in an infinite loop if there's no next page.
    $url = NULL;
    if ($response
      ->getHttpStatusCode() == Response::HTTP_OK) {
      $data = json_decode($response
        ->getBody(), TRUE);
      $posts = array_merge($this
        ->extractFacebookFeedData($post_types, $data['data']), $posts);
      $post_count = count($posts);
      if ($post_count < $num_posts && isset($data['paging']['next'])) {
        $url = $data['paging']['next'];
      }
    }
  } while ($post_count < $num_posts && NULL != $url);
  return array_slice($posts, 0, $num_posts);
}