Himanshu gupta
Himanshu gupta

Reputation: 650

facebook php sdk 4 version, unable to fetch user albums

I am developing a facebook application via php sdk 4 version.
My code is as follows:

try {
    $session = $helper->getSession();
} catch (FacebookRequestException $ex) {
    echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
if ($session) {
    try {
         $request = new FacebookRequest($session, 'GET', '/me');
         $response = $request->execute();
         $me = $response->getGraphObject();
         $user_id = $me->getProperty('id');
         echo $user_id;
         $accessToken = $session->getAccessToken();
         echo $accessToken;
         echo "<br>".$user_id;
         $request = new FacebookRequest($session, 'GET', '/me/albums');
         $response = $request->execute();
         $userAlbums = $response->getGraphObject();
         echo $userAlbums['data'][0]['id'];


    } catch(FacebookRequestException $e) {
        echo $e->getMessage();
    }
} else {
    $helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/lykebook/');
$auth_url = $helper->getLoginUrl(array('user_friends', 'publish_actions', 'user_photos', 'user_status', 'friends_photos','friends_status','publish_stream'));
echo "<script>window.top.location.href='".$auth_url."'</script>";
}


But the problem is I am not getting any album data. I don't know what the problem is? The earlier request i.e: /me is working fine. I checked that by printing $user_id. But the next request for getting albums is not working i.e /me/albums. Help me in correcting this.

Upvotes: 0

Views: 384

Answers (2)

FabioSorre
FabioSorre

Reputation: 310

You could use getGraphEdge, here below the code:

    $fb->setDefaultAccessToken($accessToken);

    try {

        $response = $fb->get('/me/albums');
        $albums = $response->getGraphEdge();

    } 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;
    }

    foreach ($albums as &$value) {
          echo $value;
    }

Upvotes: 0

ifaour
ifaour

Reputation: 38135

Try using the getGraphObjectList() method since you are expecting more than one object. Then the result will be an array of GraphObject objects, see here.

From here, you need to access these as objects and not arrays with the helper methods available (e.g. getProperty()).

Otherwise, you can retrieve the array backing this object with asArray().

Upvotes: 1

Related Questions