JeroenVdb
JeroenVdb

Reputation: 808

Get all images from specific Facebook album with Graph API PHP SDK

Hi I'm trying to grab all pictures from a specific album (always the same hardcoded id). I'm using the Graph API PHP SDK from Facebook. This is my code:

<?php
require 'phpfiles/facebook.php';

    $facebook = new Facebook(array(
    'appId'  => 'aaaa',
    'secret' => 'bbbb',
    'cookie' => true
));

$user_profile = $facebook->api('/1881235503185/photos?access_token=cccc');
var_dump($user_profile);

The var_dump output:

array(1) { ["data"]=> array(0) { } }

When I use the Javascript SDK it works fine...

FB.api('/1881235503185/photos?access_token=cccc', function(response) {
    alert(response.data[0].name);
});

Output: Diep in de put

Am I forgetting something?

Upvotes: 8

Views: 39319

Answers (3)

JeroenVdb
JeroenVdb

Reputation: 808

I got it! It should be:

$user_profile = $facebook->api('/1881235503185/photos', array('access_token' => 'cccc'));

With the new Facebook PHP SDK it should be:

$albumjson = $facebook->api('/1881235503185?fields=photos');

Upvotes: 7

Eddie
Eddie

Reputation: 13116

I find it strange that it works with JS and not PHP... Makes me think it's something to do with your PHP FB setup.. Have you tried another call to check it's not? Such as

$facebook->api('/me');

Also make sure you have checked these:

To read the 'photo' object you need

any valid access_token if it is public user_photos permission to access photos and albums uploaded by the user user_photo_video_tags permission to access photos in which the user has been tagged friends_photos permission to access friends' photos friends_photo_video_tags permission to access photos in which the user's friends have been tagged

Src:

http://developers.facebook.com/docs/reference/api/photo/

Upvotes: 2

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

<?php
    require_once 'library/facebook.php';
    try{
        $facebook = new Facebook(array(
                'appId' => $app_id,
                'secret' => $app_secret,
                'cookie' => true
        ));
        if(is_null($facebook->getUser()))
        {
                header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
                exit;
        }
        $me = $facebook->api('/me');
    }catch(Exception $e){
        echo $e->getMessage();
        echo '<p>Please try clearing your browser cookies or <a href="http://demos.frnzzz.com/fbAlbum/photos.php">click here</a>.</p>';
        die;
    }
?>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
        <script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script> 
        <script type="text/javascript"> 
        $(document).ready(function() {
            $('.slideshow').cycle({
                fx: 'fade'
            });
        });
        </script> 
        <title>WebSPeaks.in | Access facebook Albums on your site using PHP</title>
    </head>
    <body>
<?php
    $albums = $facebook->api('/me/albums');

    $action = $_REQUEST['action'];

    $album_id = '';
    if(isset($action) && $action=='viewalbum'){ 
        $album_id = $_REQUEST['album_id'];
        $photos = $facebook->api("/{$album_id}/photos");
        ?>
        <div class="slideshow"> 
        <?php
        foreach($photos['data'] as $photo)
        {
            echo "<img src='{$photo['source']}' />";
        }
        ?>
        </div>
        <?php
    }

    $pageURL .= 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    echo '<div class="alb">';
    if(strstr($pageURL,'.php?')){
        $and = '&';
    }else{
        $and = '?';
    }

    echo '<p class="hd">My Albums</p>';
    foreach($albums['data'] as $album)
    {
        if($album_id == $album['id']){
            $name = '<b><u>'.$album['name'].'</u></b>';
        }else{
            $name = $album['name'];
        }
        echo '<p>'."<a href=".$pageURL.$and."action=viewalbum&album_id=".$album['id'].">".$name.'</a></p>';
    }
    echo '</div>';
    ?>
    </body>
</html>

Upvotes: 3

Related Questions