public function index(\SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { $this->facebookModel = new Facebook(); //Verify if there is user on database $userExist = SocialWorkerController::userExist(); if ($userExist > 0) { //get all the users access tokens from the database $userAccessToken = SocialWorkerController::getUsersAccessToken(); // It will run the array if the access tokens, set them in the SDK, get the posts from Facebook // and send it to class which will extract the hashtags foreach ($userAccessToken as $key => $value) { // Set the user access token into the FAcebook SDK to perform searchs on Facebook Graph $fb->setDefaultAccessToken($value["AccessToken"]); //Get yesterday's date $date = new DateTime("yesterday"); //retrive yesterday's date in the format 9999-99-99 $yesterdayDate = $date->Format("Y-m-d"); // $yesterdayDate = "2015-11-28"; //Try to make a request to Facebook Graph try { //Make a request retrieving yesterday's posts message with a limit of 100 posts per page $requestUserPosts = $fb->get("me/posts?fields=message&since=" . $yesterdayDate . "&limit=100"); //$responses = $fb->sendBatchRequest($batch); } catch (\Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error //dd('Graph returned an error: ' . $e->getMessage()); continue; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues //dd('Facebook SDK returned an error: ' . $e->getMessage()); continue; } //Try to get posts from first to last page // Get Page 1 $feedEdge = $requestUserPosts->getGraphEdge(); $this->dispatch(new \SmartCity\Jobs\FacebookPost($feedEdge)); // Get Next pages $nextFeed = $fb->next($feedEdge); //While there is more pages keep creating new jobs to process the message while ($nextFeed !== null) { $this->dispatch(new \SmartCity\Jobs\FacebookPost($nextFeed)); $nextFeed = $fb->next($feedEdge); } } } }
function index(\SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { $fb->setDefaultAccessToken(getenv("FACEBOOK_APP_ACCESSTOKEN")); $cities = FacebookPagesController::getCities(); foreach ($cities as $key => $values) { try { //Get last saturday date $since = new DateTime("Saturday last week"); var_dump($since); // Requires the "read_stream" permission $requestPagePosts = $fb->get($values["FacebookId"] . "/posts?fields=message,created_time&limit=100&since=" . $since->Format("Y-m-d")); //2015-11-30"); } catch (\Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error dd('Graph returned an error: ' . $e->getMessage()); continue; } catch (\Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues dd('Facebook SDK returned an error: ' . $e->getMessage()); continue; } //Try to get posts from first to last page // Get Page 1 $feedEdge = $requestPagePosts->getGraphEdge(); foreach ($feedEdge as $status) { $this->dispatch(new \SmartCity\Jobs\FacebookPagePostsJob($feedEdge, $values["City"])); } // Get Next pages $nextFeed = $fb->next($feedEdge); while ($nextFeed !== null) { foreach ($nextFeed as $status) { $this->dispatch(new \SmartCity\Jobs\FacebookPagePostsJob($nextFeed, $values["City"])); } $nextFeed = $fb->next($feedEdge); } } }