Rubenwouters\CrmLauncher\ApiCalls\FetchFacebookContent::fetchPosts PHP Method

fetchPosts() public method

Fetch posts from Facebook
public fetchPosts ( datetime $newest ) : array | Illuminate\View\View
$newest datetime
return array | Illuminate\View\View
    public function fetchPosts($newest)
    {
        $fb = initFb();
        $token = $this->config->FbAccessToken();
        try {
            if ($newest) {
                $posts = $fb->get('/' . config('crm-launcher.facebook_credentials.facebook_page_id') . '/tagged?fields=from,message,created_time,full_picture&since=' . strtotime($newest), $token);
            } else {
                $posts = $fb->get('/' . config('crm-launcher.facebook_credentials.facebook_page_id') . '/tagged?fields=from,message,created_time,full_picture&limit=1', $token);
            }
            return json_decode($posts->getBody());
        } catch (Exception $e) {
            getErrorMessage($e->getCode());
            return back();
        }
    }

Usage Example

 /**
  * Gets all posts on Facebook
  * @return view
  */
 public function collectPosts()
 {
     $newestPost = $this->message->getNewestPostDate();
     $posts = $this->facebookContent->fetchPosts($newestPost);
     foreach ($posts->data as $key => $post) {
         $contact = $this->contact->createContact('facebook', $post);
         $case = $this->case->createCase('facebook_post', $post, $contact);
         $message = new Message();
         $message->contact_id = $contact->id;
         $message->fb_post_id = $post->id;
         $message->case_id = $case->id;
         if (isset($post->message)) {
             $message->message = $post->message;
         }
         $message->post_date = changeFbDateFormat($post->created_time);
         $message->save();
         $this->media->handleMedia($message->id, $post, 'facebook');
     }
     $newestComment = latestCommentDate();
     $newestInnerComment = $this->innerComment->LatestInnerCommentDate();
     $this->fetchComments($newestComment);
     $this->fetchInnerComments($newestInnerComment);
 }