我正在尝试构建一个分析仪表板,并获得页面浏览量,约会,并触及到一个Facebook页面。我还没有找到用我们的应用程序和管理用户访问图api上的read_insights的方法(参见这个问题:insights) on node type (Page)),但是我已经能够使用file_get_contents直接访问一些信息,并通过它运行一个url。我知道这个很管用:
https://graph.facebook.com/{page_id}/feed?access_token={access_token}通过那个电话,我得到了最近的帖子的排列数据。
我运行这个URL来获取洞察力,page_views_total:
https://graph.facebook.com/{page_id}/insights/page_views_total?access_token={access_token}但我只得到了一个数组
{
"data": [
],
"paging": {
"previous": "https://graph.facebook.com/v2.9/{page_id}/insights?access_token={token_string}\{access_string}w&pretty=1&metric=page_views_total&since=1497596400&until=1497769200",
"next": "https://graph.facebook.com/v2.9/{page_id}/insights?access_token={token_string}\{access_string}&pretty=1&metric=page_views_total&since=1497942000&until=1498114800"
}
}这些数组都没有任何有用的信息。我尝试过在数组中运行封闭的URL,但它只会给出Oauth错误。
我最初获得这个URL的方式是遵循以下步骤,并将feed替换为insights/{metric_data}
“去token/。当你复制它的时候,你直接去https://graph.facebook.com//feed?access_token=”
附带注意:对于新手来说,Facebook很难解释,所以我把PHP中的身份验证和令牌方法放在一边,并试图为我的数据获取一个直接的URL。也许不是最好的解决方案,但我不知道如何使用我的应用程序进行身份验证,并提取页面洞察力数据。
发布于 2017-06-23 12:50:16
一旦你有了这个标记,你就可以把一个URL串在一起,得到你需要的任何洞察力数据。例如,要获得过去一周的page_views_total:
https://graph.facebook.com/{app_id}/insights/page_views_total/week?access_token={non_expiring_token}这应该返回一个数组,如果使用PHP,您可以获取该URL上的文件内容,并使用json_decode将其读取为一个对象:
$requestedInsights = file_get_contents('https://graph.facebook.com/{app_id}/insights/page_views_total/week?access_token={non_expiring_token}');
$decodedObject = json_decode($requestedInsights);数组应该如下所示:
{
"data": [
{
"name": "page_views_total",
"period": "week",
"values": [
{
"value": 4,
"end_time": "{time_string}"
},
{
"value": 5,
"end_time": "{time_string}"
}
],
"title": "Weekly Total views count per Page",
"description": "Weekly: Total views count per Page",
"id": "{app_id}/insights/page_views_total/week"
}
],
"paging": {
"previous": "{paging_url_with_token}",
"next": "{paging_url_with_token}"
}
}然后,您可以通过以下方法获得对象中需要的特定值:
$pageViewsInsight = $decoded->data[0]->values[0]->value;我发现第一个值与我在Insight仪表板上看到的相匹配。我不知道为什么用不同的时间代码返回两个不同的值。
你可以改变每周或days_28。除了这些之外,我还没有找到使用until=或设置特定时间的方法。
若要请求多个指标,请将它们之间的逗号串在一起。
https://graph.facebook.com/{app_id}/insights/page_views_total,page_impressions_unique,page_post_engagements/week?access_token={non_expiring_token}使用此方法,您不能为每个洞察指定时间(周、日、days_28)。它将为所有请求的指标应用时间。
对于多个度量的请求,您将需要检查数据数组的名称,以获得适当的值。
{
"data": [
{
"name": "page_impressions_unique",
"period": "week",
"values": [
{
"value": 38,
"end_time": "{time_string}"
},
{
"value": 24,
"end_time": "{time_string}"
}
],
"title": "Weekly Total Reach",
"description": "Weekly: The number of people who have seen any content associated with your Page. (Unique Users)",
"id": "{app_id}/insights/page_impressions_unique/week"
},
{
"name": "page_views_total",
"period": "week",
"values": [
{
"value": 4,
"end_time": "{time_string}"
},
{
"value": 5,
"end_time": "{time_string}"
}
],
"title": "Weekly Total views count per Page",
"description": "Weekly: Total views count per Page",
"id": "{app_id}/insights/page_views_total/week"
},
{
"name": "page_post_engagements",
"period": "week",
"values": [
{
"value": 7,
"end_time": "{time_string}"
},
{
"value": 10,
"end_time": "{time_string}"
}
],
"title": "Weekly Post Engagements",
"description": "Weekly: The number of times people have engaged with your posts through like, comments and shares and more.",
"id": "{app_id}/insights/page_post_engagements/week"
}
],
"paging": {
"previous": "{paging_url_with_token}",
"next": "{paging_url_with_token}"
}
}https://stackoverflow.com/questions/44709883
复制相似问题