Reputation: 331
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.
Upvotes: 1
Views: 8014
Reputation: 3566
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 .
Upvotes: 2