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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6678756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:59:02  来源:igfitidea点击:

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

phpfacebookfacebook-graph-api

提问by JeroenVdb

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:

嗨,我正在尝试从特定相册中获取所有图片(始终使用相同的硬编码 ID)。我正在使用 Facebook 的 Graph API PHP SDK。这是我的代码:

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

var_dump 输出:

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.
  • 1881235503185是MY专辑的id,不限制,对所有人开放
  • access_token 是我从我的应用程序页面为我的 fb id 获取的令牌。我没有收到 oauth 错误。
  • 我有权限 (user_photos) 并尝试添加十几个其他权限。
  • 当我使用 Graph API Explorer 尝试它时,它可以工作。

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

当我使用 Javascript SDK 时它工作正常......

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

Output: Diep in de put

输出: 投入产出

Am I forgetting something?

我是不是忘记了什么?

采纳答案by JeroenVdb

I got it! It should be:

我知道了!它应该是:

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

With the new Facebook PHP SDK it should be:

使用新的 Facebook PHP SDK,它应该是:

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

回答by Arvind Bhardwaj

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

回答by Eddie

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

我觉得它适用于 JS 而不是 PHP 很奇怪......让我觉得这与你的 PHP FB 设置有关..你是否尝试过另一个调用来检查它不是?如

$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

要读取您需要的“照片”对象

任何有效的access_token,如果是public user_photos 访问用户上传的照片和相册的权限 user_photo_video_tags 访问用户被标记的照片的权限friends_photos 访问朋友的照片的权限friends_photo_video_tags 访问用户的朋友被标记的照片的权限

Src:

来源:

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

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