简体   繁体   中英

Facebook API for PHP: how to get user albums and photos?

Hello I'm using Facebook API for PHP (latest version) and I'm trying to get all albums with photos of an user.

I wrote this code:

<?php
require_once "common.php";

if( !isset( $_SESSION['facebook_access_token'] ) ){
    header("Location: index.php");
    exit;
}

$token= $_SESSION['facebook_access_token'];

try {
    // Returns a `Facebook\FacebookResponse` object
    $response= $fb->get('/me?fields=id,name,first_name,last_name,gender,link,birthday,location,picture', $token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$user= $response->getGraphUser();

$albums = $fb->get('/me/albums', $token);

$action = ( isset($_REQUEST['action']) ) ? $_REQUEST['action'] : null;

$album_id = '';
if($action=='viewalbum'){
    $album_id = $_REQUEST['album_id'];
    $photos = $fb->get("/{$album_id}/photos", $token);
    ?>
    <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>';

I've found this example here: Get all images from specific Facebook album with Graph API PHP SDK But is says: Cannot use object of type Facebook\\FacebookResponse as array on line 53 (I've changed but don't work at all because API function doesn't exist!)

I've also tried to follow the documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos/

But execute function doesn't exist! What shall I do?

I've solved... with $albums = $fb->get('/me/albums', $token)->getGraphEdge()->asArray(); I can get album id and name as an array. for the photos:

$album_id = $_REQUEST['album_id'];
    $photos = $fb->get("/{$album_id}/photos?fields=picture", $token)->getGraphEdge()->asArray();
    ?>
    <div class="slideshow">
        <?php
        foreach($photos as $photo)
        {
            echo " <img src='{$photo['picture']}' /> ";
        }
        ?>
    </div>

This works!

您应该使用$photos = $fb->get("/{$album_id}/photos", $token)->getGraphEdge();

Here to attach the code retrieve varies size of photo instead of only 100px images from album.

$photos = $fb->get("/{--album_id--}/photos?fields=images&width", $accessToken->getValue())->getGraphEdge()->asArray();

    //var_dump($photos);
    foreach($photos as $photo){
        echo "<br><img src='{$photo['images'][0]['source']}' />";//Get largest by 0 index
        //foreach($photo['images'] as $photosize){
        //    echo "<br><img src='{$photosize['source']}'/>";
        //}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM