如何使用 php 将照片从 facebook 导入到网站中?

发布于 2024-11-16 12:23:09 字数 170 浏览 2 评论 0原文

我正在创建一个网站,使用 Facebook 帐户登录的用户将在页面上看到他们的照片。我必须在我的网站中输入什么代码才能看到显示登录用户的图片。

如果有人可以帮助我提供教程链接或为我指出正确的方向。

我已经阅读了 Facebook 和 stack Overflow 上的很多文档,但我找不到答案,

I am creating a site where a user that logs in with their Facebook account will see their pictures on the page . What code do i have to put in my website to see the loged in users pictures diplayed.

If someone can help me with a link to a tutorial or point me in the right direction.

I have read a lot of documentation from Facebook and on stack overflow but i cant find an answer,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-11-23 12:23:09

您可以通过两种方式执行此操作:在服务器端(在您的情况下使用 PHP)或使用 JavaScript SDK 在客户端。

两者都假设您拥有所需的访问凭据。您需要注册一个应用程序帐户才能在 Facebook 开发者网站

服务器端

获取这些内容第一步是让您的应用程序参与 OAuth 身份验证过程。 Facebook 指南 中有关于 PHP 的详细记录(请参阅服务器端流程部分) 。

完成此操作后,您将获得一个访问令牌,您可以使用它调用 Graph API。获取用户照片的端点是https://graph.facebook.com/me/photos?access_token=。在这种情况下,me 始终是登录并为您的应用程序提供令牌的用户。

在 PHP 中,假设您已将访问令牌存储在 $SESSION['token'] 中,您可以使用以下方式发出照片负载请求:

$url = "https://graph.facebook.com/me/photos?access_token=" . $SESSION['token'];
$photos = json_decode(file_get_contents($url));

$photos 对象将是Facebook 文档<中描述的Photo实体列表/a>.

客户端

您需要在网页上设置 JavaScript SDK,如此处。

客户端的身份验证由 JavaScript SDK 处理,再次记录在身份验证指南中。

使用 SDK,您可以对 Graph API 进行客户端 JavaScript 调用以获取相同的照片结构:

FB.api('/me/photos', function(response) {
    if(!response || response.error) {
        // render error
    } else {
        // render photos
    }
});

You can do this two ways: on the server-side (in PHP in your case) or on the client-side with the JavaScript SDK.

Both assume you have the required access credentials. You need to sign up for an application account to get these at the Facebook Developer site

Server-Side

First-step is to get your application to participate in the OAuth authentication process. This is well-documented for PHP in the Facebook guide (see the Server-Side Flow section).

Once you've done that, you'll have an access token that you can call into the Graph API with. The endpoint to get the user's photos is https://graph.facebook.com/me/photos?access_token=<token>. In this case the me, is always the user who signed in to give your application the token.

In PHP, assuming you've stored the access token in $SESSION['token'] you can make a request for the photos payload with:

$url = "https://graph.facebook.com/me/photos?access_token=" . $SESSION['token'];
$photos = json_decode(file_get_contents($url));

The $photos object will be a list of Photo entities that are described in the Facebook docs.

Client-Side

You'll need to setup the JavaScript SDK on your web pages as documented here.

Authentication on the client-side is handled by JavaScript SDK, again documented in the authentication guide.

Using the SDK, you can make a client-side JavaScript call to the Graph API for the same photos structure:

FB.api('/me/photos', function(response) {
    if(!response || response.error) {
        // render error
    } else {
        // render photos
    }
});
面犯桃花 2024-11-23 12:23:09

检查堆栈上的另一个问题如何从 Facebook 导入照片?

Check this other question on stack How to import photos from Facebook?

七色彩虹 2024-11-23 12:23:09

我认为现在 Facebook 已经改变了导入照片的方式,所以我们必须首先获取相册,而不是导入该相册的照片。至少我是这样做到的。 :) 下面是使用 PHP 获取相册的基本 api 调用

:- api('/me/albums', $params); ?>
获取相册照片 :- api('/' . $album_id . '/photos', $params); ?>

现在这是完整的代码摘要。请将此代码复制到文件中并检查是否执行导入照片

<?php
include 'facebook/facebook.php';
$config = array();
$config['appId'] = YOUR_APP_ID;
$config['secret'] = YOUR_APP_SECRET;
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
?>
<?php
if ($user_id && $access_token) {

    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        $params = array(
            'method' => 'get',
            'access_token' => $access_token
        );

        if (isset($_GET['aid']) && $_GET['aid'] != '') {
            $aid = $_GET['aid'];
            $user_album_photos = $facebook->api('/' . $aid . '/photos', $params);
            //echo "Photos<br/>"; 
            ?>

            <?php foreach ($user_album_photos['data'] as $key => $value) {
                ?>
                <div class="album">
                    <div class="frame photo_frame">
                        <div class="edit-photo-nohover" style="display:block">
                            <div><input type="checkbox" id="fbimport_id<?php echo $value['id']; ?>" value="<?= $value['id'] . ',' . $value['images']['0']['source'] . ',' . $value['name'] ?>" name="fbimport[]" > <span>Import this Memory</span></div>

                        </div>
                        <table class="test">
                            <tr><td>
                                    <a href="javascript:void(0)"><img src="<?= $value['images']['0']['source'] ?>" height="100" width="100" /></a>
                                </td>
                            </tr>
                        </table>
                        <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                    </div><br/>
                </div>
                <?php }
            ?>


        <?php
        } else {
            $user_albums = $facebook->api('/me/albums', $params);
            echo '<h3 class="page-title">Select Your Facebook Album</h3><br/><br/>';
            foreach ($user_albums['data'] as $key => $value) {

                /* load album if not blank */
                if (isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count'] > 0) {

                    /* check if album has a cover photo. if not than load a default image */
                    if (isset($value['cover_photo']) && $value['cover_photo'] != '' && $value['cover_photo'] != NULL) {
                        $user_album_cover = $facebook->api('/' . $value['cover_photo'], $params);
                        $album_thumbnail = $user_album_cover['images']['0']['source'];
                    } else {
                        $album_thumbnail = 'default_thumb.gif';
                    }
                    /* check if album has cover photo end */
                    ?>
                    <div class="album">
                        <div class="frame photo_frame">
                            <table class="test">
                                <tr><td>
                                        <a href="?aid=<?= $value['id'] ?>" ><img src="<?= $album_thumbnail ?>" height="100" width="100" /></a>
                                    </td>
                                </tr>
                            </table>
                            <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                        </div><br/>
                    </div>

                    <?php
                }//if(isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count']>0)
                /* load album if not blank end */
            }
        }
    } catch (FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl();
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, print a link for the user to login
    $login_url = $facebook->getLoginUrl();
    echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>

I think Now Facebook has changed the way to import photos and so we have to first get albums and than to import photos of that album. At-least i made it in this way. :) Below are the basic api call using PHP

get albums :- <?php $user_albums = $facebook->api('/me/albums', $params); ?>
get album photos :- <?php $user_album_photos = $facebook->api('/' . $album_id . '/photos', $params); ?>

Now here is a full code summary. Do copy this code in file and check for doing import photos

<?php
include 'facebook/facebook.php';
$config = array();
$config['appId'] = YOUR_APP_ID;
$config['secret'] = YOUR_APP_SECRET;
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
?>
<?php
if ($user_id && $access_token) {

    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        $params = array(
            'method' => 'get',
            'access_token' => $access_token
        );

        if (isset($_GET['aid']) && $_GET['aid'] != '') {
            $aid = $_GET['aid'];
            $user_album_photos = $facebook->api('/' . $aid . '/photos', $params);
            //echo "Photos<br/>"; 
            ?>

            <?php foreach ($user_album_photos['data'] as $key => $value) {
                ?>
                <div class="album">
                    <div class="frame photo_frame">
                        <div class="edit-photo-nohover" style="display:block">
                            <div><input type="checkbox" id="fbimport_id<?php echo $value['id']; ?>" value="<?= $value['id'] . ',' . $value['images']['0']['source'] . ',' . $value['name'] ?>" name="fbimport[]" > <span>Import this Memory</span></div>

                        </div>
                        <table class="test">
                            <tr><td>
                                    <a href="javascript:void(0)"><img src="<?= $value['images']['0']['source'] ?>" height="100" width="100" /></a>
                                </td>
                            </tr>
                        </table>
                        <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                    </div><br/>
                </div>
                <?php }
            ?>


        <?php
        } else {
            $user_albums = $facebook->api('/me/albums', $params);
            echo '<h3 class="page-title">Select Your Facebook Album</h3><br/><br/>';
            foreach ($user_albums['data'] as $key => $value) {

                /* load album if not blank */
                if (isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count'] > 0) {

                    /* check if album has a cover photo. if not than load a default image */
                    if (isset($value['cover_photo']) && $value['cover_photo'] != '' && $value['cover_photo'] != NULL) {
                        $user_album_cover = $facebook->api('/' . $value['cover_photo'], $params);
                        $album_thumbnail = $user_album_cover['images']['0']['source'];
                    } else {
                        $album_thumbnail = 'default_thumb.gif';
                    }
                    /* check if album has cover photo end */
                    ?>
                    <div class="album">
                        <div class="frame photo_frame">
                            <table class="test">
                                <tr><td>
                                        <a href="?aid=<?= $value['id'] ?>" ><img src="<?= $album_thumbnail ?>" height="100" width="100" /></a>
                                    </td>
                                </tr>
                            </table>
                            <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                        </div><br/>
                    </div>

                    <?php
                }//if(isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count']>0)
                /* load album if not blank end */
            }
        }
    } catch (FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl();
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, print a link for the user to login
    $login_url = $facebook->getLoginUrl();
    echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文