I want to use Facebook PHP SDK v5 to get a list of photo albums for a Page.
I am following the instructions at https://developers.facebook.com/docs/graph-api/reference/v2.4/page/albums, which are as follows:
/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/{page-id}/albums'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
This does however seem to be incorrect as there is no "execute
" function in the FacebookRequest class.
In addition, there also needs to be the access_token
passed as the second variable to the FacebookRequest
construct.
My full code is as follows:
require_once("classes/Facebook/autoload.php");
use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
$config = array();
$config['app_id'] = '{app_id}';
$config['app_secret'] = '{secret}';
$config['default_graph_version'] = 'v2.4';
$fb = new Facebook($config);
$app= new FacebookApp($config['app_id'],$config['app_secret']);
$request=new FacebookRequest($app,"{access_token}",'GET','/{page_id}/albums');
$response = $request->execute();
$graphObject = $response->getGraphObject();
print_r($graphObject);
This just produces the
fatal error: Call to undefined method Facebook\FacebookRequest::execute()
Can someone point me in the direction of the correct code?
Have a look at the docs at
Sample code:
$fbApp = new Facebook\FacebookApp('{app-id}', '{app-secret}');
$request = new Facebook\FacebookRequest($fbApp, '{access-token}', 'GET', '/{page_id}/albums');
// OR
$fb = new Facebook\Facebook(/* . . . */);
$request = $fb->request('GET', '/{page_id}/albums');
// 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->getGraphNode();
print_r($graphNode);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With