使用Graph API PHP SDK从特定的Facebook相册中获取所有图像

使用Graph API PHP SDK从特定的Facebook相册中获取所有图像

问题描述:

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) { } }
  • 1881235503185 is the id of MY album that is not restricted, it's open to everybody
  • the access_token is the token I get from my application page for my fb id. I don't get oauth errors.
  • I have the permissions (user_photos) and tryed to add a dozen of other permissions.
  • When I try it with the Graph API Explorer it works to.

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?

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');

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/

<?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>