使用Facebook API获得帖子视图的更有效方法 [英] A more efficient way to get post views using the facebook api
问题描述
我正在使用facebook api从页面 insights
对象读取 post_impressions
字段.问题是,您只能备份一个帖子的结果,或者显示一个月/周/日所有帖子的所有印象的结果,这意味着您无法获取显示所有帖子及其相应 post_impressions
.
I am using the facebook api to read the post_impressions
field from a pages insights
object. The problem is, you can only back a result for 1 single post, or a result that shows all impressions from all posts in a month/week/day, this means you cannot get back a result that shows all posts with their corresponding post_impressions
.
我现在想要使它工作的方法是循环我的facebook帖子,在该循环中,我为 insights
对象放置了一个file_get_contents,然后在URL中添加了一个循环的帖子ID.
The way I got it to work as I want now is by looping my facebook posts and within that loop I put a file_get_contents for the insights
object and just add a looped post id inside the url.
这样,我收到的每条帖子都会向FB api发出新请求,这将导致大量的加载时间.
This way every post I get, a new request is made to the FB api which takes a heavy toll on loading times.
有没有更有效的方法来做到这一点?当我删除代码的那一部分时,我的网站加载得更快.
Is there a more efficient way to do this? When I remove that part of my code, my site loads way faster.
下面是我的代码摘要,以使您更好地理解:
Here is a summary of my code to get a better idea:
$json_object = file_get_contents("https://graph.facebook.com/v2.10/367712570004555/posts?fields=comments,full_picture,likes,message,picture,story,permalink_url,updated_time,from&access_token=12303302670122ZDzN5ucp8JzMf5VBDdY");
$feedarray = json_decode($json_object);
foreach ($feedarray->data as $key => $feed_data)
{
$json_insights = file_get_contents("https://graph.facebook.com/v2.10/".$feed_data->id."/insights/post_impressions?access_token=EAACEdEose0cBANjKErkMLhCgmZBBrgLn8KtZBW2d6JbWz7sZAivld67JxvP8MqyZCs9xg3Bh11lzETOwd64LIpAPp3zbstu8ELFZC8HOwYyxckqJ60WQcVL1FqB3iy5L3oRCsFSQA7MzHGgcqIYxsjAlJomYrOMZCqo6PwZDZD");
$views = $insights->data[0]->values[0]->value;
echo $views;
}
推荐答案
您可以使用字段扩展语法可一次性完成所有操作:
You can use Field Expansion syntax to get this all in one go:
.../367712570004555/posts?fields=comments,...,insights.metric(post_impressions)
这篇关于使用Facebook API获得帖子视图的更有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!