Get facebook page rate and reviews using Graph api

喜欢而已 提交于 2019-12-03 03:57:36
Sascha

I had the same problem and I answered my own question here:

you need to call {page-id}/ratings?field=open_graph_story, but you need the access token of your page for that (use /me/accounts in the Graph API Explorer to get the token). You find further information in the facebook documentation.

This is an old post, but I wanted to give some extra info to others that might be looking for a solution to this problem still. As stated in the Facebook Open Graph documentation, you definitely need a page access token to get the page's individual ratings/reviews. If you are looking for the overall rating and the count of reviews, however, you are in luck. By simply making a call to /{page-id}?fields=overall_star_rating,rating_count, you will be able to access that data.

Here's an example response:

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

David R. Myers II's answer is just what I needed. Thank you! You can get the overall_star_rating and the rating_count fields of any page.

You need to submit your app for review by Facebook and request to use the Page Public Content Access feature.

For developing, while your Facebook app status is In development you automatically have access to the public content for pages that you own. Before going live, request a review from Facebook so you can access other pages.

To list pages you own: https://www.facebook.com/bookmarks/pages?ref=u2u

Sample code to get the ratings:

// 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' );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!