我在集成了php图形sdk的laravel应用程序中使用了facebook graph api。我有一个统计页面,我在moris图表中显示每种类型的帖子数,排名前3的帖子以及一些见解指标,例如“ page_post_engagements”。 这是我的控制器:
public function stats($id,Facebook $fb)
{
$page = Page::find($id);
$page_fb_id = $page->fb_id;
$page_access_token = $page->access_token;
$oAuth2Client = $fb->getOAuth2Client();
$fb->setDefaultAccessToken($oAuth2Client->getLongLivedAccessToken($page_access_token)->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()->subDays('29');
$endDate = Carbon::now();
$numberOfDays = $endDate->diffInDays($startDate);
$classement = array();
$posts = $fb->get('/'.$page_fb_id.'/posts?fields=type&since='.Carbon::parse($startDate).'&until='.Carbon::parse($endDate))->getGraphEdge();
if(empty($posts))
{
$classement = ['total'=>0,'likes'=>0,'comments'=>0,'shares'=>0];
}
else
{
foreach ($posts as $key => $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->get('/'.$post['id'].'/insights?metric=post_impressions_unique')->getGraphEdge();
$engage = $fb->get('/'.$post['id'].'/insights?metric=post_engaged_users')->getGraphEdge();
$classement[] = ['total'=>($engage[0]['values'][0]['value']/$impress[0]['values'][0]['value'])*100,'id'=>$post['id'],'impressions'=>$impress[0]['values'][0]['value'],'engage'=>$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->get('/'.$end_elem['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')->getGraphNode();
$likes1 = $fb->get('/'.$end_elem['id'].'/likes?limit=1000000')->getGraphEdge()->count();
$comments1 = $fb->get('/'.$end_elem['id'].'/comments?limit=1000000')->getGraphEdge()->count();
$shares1 = $fb->get('/'.$end_elem['id'].'/sharedposts?limit=1000000')->getGraphEdge()->count();
}
if($mid_elem[0]['id'])
{
$top2 = $fb->get('/'.$mid_elem[0]['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')->getGraphNode();
$likes2 = $fb->get('/'.$mid_elem[0]['id'].'/likes?limit=1000000')->getGraphEdge()->count();
$comments2 = $fb->get('/'.$mid_elem[0]['id'].'/comments?limit=1000000')->getGraphEdge()->count();
$shares2 = $fb->get('/'.$mid_elem[0]['id'].'/sharedposts?limit=1000000')->getGraphEdge()->count();
}
if($start_elem[0]['id'])
{
$top3 = $fb->get('/'.$start_elem[0]['id'].'/?fields=id,message,full_picture,source,type,created_time,from{name,picture}')->getGraphNode();
$likes3 = $fb->get('/'.$start_elem[0]['id'].'/likes?limit=1000000')->getGraphEdge()->count();
$comments3 = $fb->get('/'.$start_elem[0]['id'].'/comments?limit=1000000')->getGraphEdge()->count();
$shares3 = $fb->get('/'.$start_elem[0]['id'].'/sharedposts?limit=1000000')->getGraphEdge()->count();
}
}
foreach (range(0, $numberOfDays) as $day)
{
$a[] = ['year'=>$endDate->copy()->subDays($day)->format('Y-m-d')];
}
foreach (array_reverse($a) as $key => $value)
{
$page_fans = $fb->get('/'.$page_fb_id.'/insights?metric=page_fans&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$fans[] = ['year'=>$value['year'],'value'=>$page_fans];
$page_post_engagements = $fb->get('/'.$page_fb_id.'/insights?metric=page_post_engagements&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$post_engagements[] = ['year'=>$value['year'],'value'=>$page_post_engagements];
$page_impressions = $fb->get('/'.$page_fb_id.'/insights?metric=page_impressions&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$impressions[] = ['year'=>$value['year'],'value'=>$page_impressions];
$page_actions_post_reactions_like_total = $fb->get('/'.$page_fb_id.'/insights?metric=page_actions_post_reactions_like_total&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$post_reactions_like_total[] = ['year'=>$value['year'],'value'=>$page_actions_post_reactions_like_total];
/*$page_engaged_users = $fb->get('/'.$page_fb_id.'/insights?metric=page_engaged_users&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$engaged_users[] = ['year'=>$value['year'],'value'=>$page_engaged_users];
$page_views_total = $fb->get('/'.$page_fb_id.'/insights?metric=page_views_total&since='.Carbon::parse($value['year']).'&until='.Carbon::parse($value['year'])->addDays('2'))->getGraphEdge()[0]['values'][0]['value'];
$views_total[] = ['year'=>$value['year'],'value'=>$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')));
}
有没有一种方法可以优化我的代码,以便获得更好的响应时间,特别是避免在10000毫秒异常后连接超时?
答案 0 :(得分:0)
我假设要获取API数据并对其进行解析...花费的时间超过10秒?
您需要做两件事来改善您的系统:1)使用缓存2)使用队列。
缓存! (将API /解析的结果保存15分钟左右)
排队! (API解析是在服务器后台完成的)
您的系统工作流程将是:
用户点击了/stats
是否有缓存版本?用它。响应时间大约只有500毫秒。
否/缓存已过期?触发排队作业以获取新的API数据。
显示用户“正在刷新数据!”并显示最新的缓存。