How can I get Facebook page reviews in PHP?

I have created a facebook app and approved the app to access manage_pages.I am looking for php code to get page access from page to get the page information.

For getting reviews and rating I am using the below code

require 'facebook-php-sdk-master/src/facebook.php';



$config = array();
$config['appId'] = '1489047331XXXXX';
$config['secret'] = '6ac210360aad27ab1044e4201XXXX';

$facebook = new Facebook($config);

print_r($facebook);

try {
	// 466400200079875 is Facebook id of Fan page https://www.facebook.com/pontikis.net
	$ret = $facebook->api("/page_id/ratings?field=open_graph_story", 'GET');
	print_r($ret);
} catch(Exception $e) {
	echo $e->getMessage();
}

I am getting the below error

(#210) This call requires a Page access token.

Any help will be highly appreciated.

asked Apr 5, 2016 at 13:07

How can I get Facebook page reviews in PHP?

4

Create new object like this and set access_token if not exists:

        $fb = new Facebook([
        'app_id' => FB_APP_ID,
        'app_secret' => FB_APP_SECRET,
        'default_graph_version' => 'v2.5',
        'default_access_token' => isset($_SESSION['facebook_access_token']) ?
            $_SESSION['facebook_access_token'] : FB_APP_ID . '|'. FB_APP_SECRET
    ]);

Change FB_APP_ID and FB_APP_SECRET, only with yours. Now you have access token, after that you can make requests and get data that u need access token for it like this (for the example):

    $request = $fb->request('GET', '/'.$page_id.'/');
    // Send the request to Graph
    try {
        $response = $fb->getClient()->sendRequest($request);
    } 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;
    }

    $graphNode = $response->getGraphPage();

    var_dump($graphNode->all());

Where $page_id is id of some page, where you can find it with its FB page URL .

answered Apr 16, 2016 at 5:46

gdfgdfggdfgdfg

2,8516 gold badges35 silver badges69 bronze badges

883 votes

0 answers

How can I get Facebook page reviews in PHP?

How can I get Facebook page reviews in PHP?

How can I get Facebook page reviews in PHP?

Get the solution ↓↓↓

I am trying to pull back an array of customer reviews (ratings?) from a particular facebook page.

I understand I need themanage_pages permission to do this, however according to the facebook docs I need to submit my app for review, providing the steps needed to reproduce the use of this permission, along with a screencast of the permission in use.

The problem I'm having is that I cannot use the permission to provide the steps and screencast because any attempt to do so results in the API telling me that it needs that permission. So a bit of a paradox really.

I assume there must be some kind of way to reproduce the steps needed in a test environment, but setting up a test app and using theapp_id andapp_secret from the test app still resulted in the following error:

Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookAuthorizationException' with message '(#200) Requires manage_pages permission to manage the object'

My code is as follows:

<?php
$fb = new Facebook\Facebook([
  'app_id' => $test_app_id,
  'app_secret' => $test_app_secret,
  'default_graph_version' => 'v2.8',
]);

if (isset($_GET['code'])) {
    $helper = $fb->getRedirectLoginHelper();
    try {
      $accessToken = $helper->getAccessToken();
    } 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;
    }

    if (isset($accessToken)) {
      // Logged in!
      $_SESSION['facebook_access_token'] = (string) $accessToken;
    }
}

if (!isset($_SESSION['facebook_access_token'])) {
    $helper = $fb->getRedirectLoginHelper();
    $permissions = ['manage_pages'];
    $loginUrl = $helper->getLoginUrl($site_url.'/fb-test.php', $permissions);
    echo '<a href="' . $loginUrl . '" target="_blank">Log in with Facebook!</a>';
    exit;
}
$access_token = $_SESSION['facebook_access_token'];

$response = $fb->get('/'.$page_id.'?fields=access_token', $access_token);
$page_access_token = $response->getDecodedBody()['access_token'];

$ratings = $fb->get('/'.$page_id.'/ratings', $page_access_token);

var_dump($ratings);

Edit (following comment from luschn)

I have altered my code to include the following, but I still get told thatmanage_pages permission is required.

<?php
$response = $fb->sendRequest(
    'GET',
    '/'.$page_id.'?fields=access_token',
    array (
        'user' => $user_id,
        'role' => 'administrators',
        'state' => rand(0,9999999)
    ),
    $access_token
);
$page_access_token = $response->getDecodedBody()['access_token'];

$response = $fb->sendRequest(
    'GET',
    '/'.$page_id.'/ratings',
    array (
        'user' => $user_id,
        'role' => 'administrators',
    ),
    $page_access_token
);
echo '<pre>'.print_r($response,true).'</pre>';
exit;

2022-04-7




People are also looking for solutions of the problem: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

How do I get Facebook Reviews on my page?

To turn on Facebook reviews:.
Go to your brand page..
Click on the “Settings” button on the Manage Page section..
A new window should appear. Click on the “Templates and Tabs” section..
Toggle the “Reviews” option to ON..

How do I get Reviews on Facebook API?

You can get all reviews from a single place by using the cursor parameter. Unlike other integrations, the facebook api doesn't support the offset parameter to paginate the results. Instead the pages need to be iterated one by one. To do this, each response comes with a next_page_cursor field.