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

fetchFbStats() public method

Fetch Facebook posts
public fetchFbStats ( object $post ) : array | Illuminate\View\View
$post object
return array | Illuminate\View\View
    public function fetchFbStats($post)
    {
        $fb = initFb();
        $token = $this->config->FbAccessToken();
        try {
            $object = $fb->get('/' . $post->fb_post_id . '?fields=shares,likes.summary(true)', $token);
            return json_decode($object->getBody());
        } catch (Exception $e) {
            getErrorMessage($e->getCode());
            return back();
        }
    }

Usage Example

 /**
  * Update stats in DB
  * 
  * @return void
  */
 public function updateFbStats()
 {
     $posts = $this->publishment->orderBy('id', 'DESC')->where('fb_post_id', '!=', '')->get();
     foreach ($posts as $key => $post) {
         $object = $this->facebookContent->fetchFbStats($post);
         if (isset($object->shares)) {
             $post->facebook_shares = $object->shares->count;
         }
         $post->facebook_likes = $object->likes->summary->total_count;
         $post->save();
     }
 }