8

I am creating website to search places using graph API. I got the places details from graph API. Is there any way to get the page rating and reviews of the places via graph API?

user3082327
  • 81
  • 1
  • 1
  • 3
  • 1
    I just answered this question in http://stackoverflow.com/questions/17315839/get-facebook-graph-api-page-review/20797173#20797173. Hope this helps. – Mahesh Dec 27 '13 at 08:38

3 Answers3

8

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.

Community
  • 1
  • 1
Sascha
  • 311
  • 3
  • 8
  • Thanks Sascha. Reminded me I needed to use the page token not the main access token. – Adam Mar 11 '14 at 06:01
  • Sascha, I am trying your answer but it is not working for me. My app have manage_pages permission. could you help with . I am getting error '''This call requires a Page access token." and code is – Gaurav Sharma Apr 01 '16 at 07:20
  • getAccessToken(); $facebook->setAccessToken($access_token); print_r($facebook); try { $ret = $facebook->api("/1633988186866636/ratings?field=open_graph_story", 'GET'); print_r($ret); } catch(Exception $e) { echo $e->getMessage(); } – Gaurav Sharma Apr 01 '16 at 07:21
6

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}"
}
davidmyersdev
  • 1,042
  • 8
  • 10
  • Hi David, can you please elaborate? E.g. how would I get those 2 fields with PHP, I tried the following code but it didn't work (I'm a bit of a newbie): $raw = file_get_contents('https://facebook.com/Yuga-Floral-Design-by-Setsuko-382079515143204?fields=overall_star_rating,rating_count'); $obj = json_decode($raw); echo $obj->overall_star_rating; – user6122500 Jan 10 '18 at 00:59
  • Hi David, I need some help regarding the facebook API. Can we access ratings of any public page ? I know to access the ratings we need page id and page access token. Suppose I have page id (of some random public page of Facebook), can I generate the access token using /me/accounts in the Graph API Explorer ? And then can I fetch the ratings of that page ? Please help. – Mansi May 25 '20 at 14:24
0

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' );
baras
  • 1
  • 1