title: How to avoid Connection timed out after 10000 milliseconds?
body: <p>I have a laravel application in wich i integrated php graph sdk to use facebook graph api. I have a stats page, i display number of posts per type , top 3 posts, and some insights metric like "page_post_engagements" ... in moris charts.
  this is my controller : </p>
  
  <pre><code>public function stats($id,Facebook $fb)
  {
      $page              = Page::find($id);
      $page_fb_id        = $page-&gt;fb_id;
      $page_access_token = $page-&gt;access_token;
      $oAuth2Client      = $fb-&gt;getOAuth2Client();
      $fb-&gt;setDefaultAccessToken($oAuth2Client-&gt;getLongLivedAccessToken($page_access_token)-&gt;getValue());
      $nb_photos   = '0';
      $nb_videos   = '0';
      $nb_links    = '0';
      $nb_likes    = '0';
      $nb_comments = '0';
      $nb_shares   = '0';
      $count_posts = '0';
      $startDate   = Carbon::now()-&gt;subDays('29');
      $endDate     = Carbon::now();
      $numberOfDays = $endDate-&gt;diffInDays($startDate);
      $classement = array();
      $posts = $fb-&gt;get('/'.$page_fb_id.'/posts?fields=type&amp;since='.Carbon::parse($startDate).'&amp;until='.Carbon::parse($endDate))-&gt;getGraphEdge();
      if(empty($posts))
      {
          $classement = ['total'=&gt;0,'likes'=&gt;0,'comments'=&gt;0,'shares'=&gt;0];
      }
      else
      {
          foreach ($posts as $key =&gt; $post) 
          {
  
              if($post['type'] == 'photo')
              {
                  $nb_photos++;
              }
              else if($post['type'] == 'video')
              {
                  $nb_videos++;
              }
              else if($post['type'] == 'link')
              {
                  $nb_links++;
              }
              $count_posts++;
              $impress      = $fb-&gt;get('/'.$post['id'].'/insights?metric=post_impressions_unique')-&gt;getGraphEdge();
              $engage       = $fb-&gt;get('/'.$post['id'].'/insights?metric=post_engaged_users')-&gt;getGraphEdge();
              $classement[] = ['total'=&gt;($engage[0]['values'][0]['value']/$impress[0]['values'][0]['value'])*100,'id'=&gt;$post['id'],'impressions'=&gt;$impress[0]['values'][0]['value'],'engage'=&gt;$engage[0]['values'][0]['value']];
  
          }
          if(is_array($classement))
          {
              asort($classement);
              $tops       = array_slice($classement, -3, 3);
              $start_elem = array_slice($tops, 0, 1);
              $mid_elem   = array_slice($tops, 1, 1);
              $end_elem   = end($tops);
          }
          if($end_elem['id'])
          {
  
              $top1 = $fb-&gt;get('/'.$end_elem['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')-&gt;getGraphNode(); 
              $likes1        = $fb-&gt;get('/'.$end_elem['id'].'/likes?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $comments1     = $fb-&gt;get('/'.$end_elem['id'].'/comments?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $shares1       = $fb-&gt;get('/'.$end_elem['id'].'/sharedposts?limit=1000000')-&gt;getGraphEdge()-&gt;count();
          }
          if($mid_elem[0]['id'])
          {
              $top2 = $fb-&gt;get('/'.$mid_elem[0]['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')-&gt;getGraphNode();
              $likes2        = $fb-&gt;get('/'.$mid_elem[0]['id'].'/likes?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $comments2     = $fb-&gt;get('/'.$mid_elem[0]['id'].'/comments?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $shares2       = $fb-&gt;get('/'.$mid_elem[0]['id'].'/sharedposts?limit=1000000')-&gt;getGraphEdge()-&gt;count();
          }
          if($start_elem[0]['id'])
          {
              $top3 = $fb-&gt;get('/'.$start_elem[0]['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')-&gt;getGraphNode();
              $likes3        = $fb-&gt;get('/'.$start_elem[0]['id'].'/likes?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $comments3     = $fb-&gt;get('/'.$start_elem[0]['id'].'/comments?limit=1000000')-&gt;getGraphEdge()-&gt;count();
              $shares3       = $fb-&gt;get('/'.$start_elem[0]['id'].'/sharedposts?limit=1000000')-&gt;getGraphEdge()-&gt;count();
          }
      }
  
  
      foreach (range(0, $numberOfDays) as $day) 
      {
          $a[] = ['year'=&gt;$endDate-&gt;copy()-&gt;subDays($day)-&gt;format('Y-m-d')];
      }
  
      foreach (array_reverse($a) as $key =&gt; $value) 
      {
          $page_fans = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_fans&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $fans[]    = ['year'=&gt;$value['year'],'value'=&gt;$page_fans];
  
          $page_post_engagements = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_post_engagements&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $post_engagements[]    = ['year'=&gt;$value['year'],'value'=&gt;$page_post_engagements];
  
          $page_impressions = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_impressions&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $impressions[]    = ['year'=&gt;$value['year'],'value'=&gt;$page_impressions];
  
          $page_actions_post_reactions_like_total = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_actions_post_reactions_like_total&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $post_reactions_like_total[]            = ['year'=&gt;$value['year'],'value'=&gt;$page_actions_post_reactions_like_total];
  
          /*$page_engaged_users = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_engaged_users&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $engaged_users[]    = ['year'=&gt;$value['year'],'value'=&gt;$page_engaged_users];
  
          $page_views_total = $fb-&gt;get('/'.$page_fb_id.'/insights?metric=page_views_total&amp;since='.Carbon::parse($value['year']).'&amp;until='.Carbon::parse($value['year'])-&gt;addDays('2'))-&gt;getGraphEdge()[0]['values'][0]['value'];
          $views_total[]    = ['year'=&gt;$value['year'],'value'=&gt;$page_views_total];*/
  
      }
      /*$fans_total                = end($fans)['value'];
      $moyenne_interaction       = array_sum(array_column($engaged_users,'value'))/count(array_column($engaged_users,'value'));
      $average_interaction       = array_sum(array_column($engaged_users,'value'))/count(array_column($engaged_users,'value'))/(end($fans)['value']);*/
      $fans                      = json_encode($fans);
      $post_engagements          = json_encode($post_engagements);
      $impressions               = json_encode($impressions);
      $post_reactions_like_total = json_encode($post_reactions_like_total);
      /*$engaged_users             = json_encode($engaged_users);
      $views_total               = json_encode($views_total);*/
      return view('stats',with(compact('randon','id','tasks','page','fans','impressions','post_engagements','post_reactions_like_total','engaged_users','views_total','nb_photos','nb_videos','nb_links','count_posts','fans_total','top1','likes1','comments1','shares1','top2','likes2','comments2','shares2','top3','likes3','comments3','shares3')));
  }
  </code></pre>
  
  <p>Is there a way to optimize my code so i can get a better response time and especially avoid the Connection timed out after 10000 milliseconds exception ?</p>