使用Graph api获取facebook页面评价和评论

问题描述 投票:8回答:3

我正在创建使用图API API搜索地点的网站。我从图API中获取了地方详细信息。有没有办法通过图形API获取页面评级和地点评论?

facebook facebook-graph-api facebook-page
3个回答
8
投票

我有同样的问题,我回答了我自己的问题here

你需要调用{page-id} / ratings?field = open_graph_story,但你需要你的页面的访问令牌(在Graph API Explorer中使用/ me / accounts来获取令牌)。您可以在facebook documentation找到更多信息。


6
投票

这是一篇旧帖子,但我想给其他可能正在寻找解决这个问题的解决方案。正如Facebook Open Graph documentation所述,您肯定需要一个页面访问令牌来获取页面的个人评级/评论。但是,如果您正在寻找整体评分和评论数量,那么您很幸运。只需调用/{page-id}?fields=overall_star_rating,rating_count,您就可以访问该数据。

这是一个示例响应:

{
    "overall_star_rating": 4.7,
    "rating_count": 31,
    "id": "{page-id}"
}

0
投票

David R. Myers II的回答正是我所需要的。谢谢!你可以得到任何页面的overall_star_ratingrating_count字段。

您需要提交您的应用以供Facebook审核并请求使用Page Public Content Access功能。

对于开发,当您的Facebook应用程序状态为In development时,您可以自动访问您拥有的页面的公共内容。在上线之前,请从Facebook请求审核,以便您可以访问其他页面。

列出您拥有的页面:https://www.facebook.com/bookmarks/pages?ref=u2u

获取评分的示例代码:

// Assuming the Facebook PHP SDK was installed with Composer.
require_once __DIR__ . '/vendor/autoload.php'; // Change path as needed.

$fb = new \Facebook\Facebook([
    'app_id' => '{app-id}',
    'app_secret' => '{app-secret}',
    'default_graph_version' => 'v3.2',
    'default_access_token' => '{access-token}',
]);

try {
    // Get the \Facebook\GraphNodes\GraphPage object for the needed page.
    $response = $fb->get('/{page-id}?fields=overall_star_rating,rating_count');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

// Get the response typed as a GraphPage.
$page = $response->getGraphPage();

// Get the needed field values.
$facebook_rating_count = $page->getField( 'rating_count' );
$facebook_overall_star_rating = $page->getField( 'overall_star_rating' );
© www.soinside.com 2019 - 2024. All rights reserved.