如何使用 php 将照片从 facebook 导入到网站中?
我正在创建一个网站,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过两种方式执行此操作:在服务器端(在您的情况下使用 PHP)或使用 JavaScript SDK 在客户端。
两者都假设您拥有所需的访问凭据。您需要注册一个应用程序帐户才能在 Facebook 开发者网站
服务器端
获取这些内容第一步是让您的应用程序参与 OAuth 身份验证过程。 Facebook 指南 中有关于 PHP 的详细记录(请参阅服务器端流程部分) 。
完成此操作后,您将获得一个访问令牌,您可以使用它调用 Graph API。获取用户照片的端点是
https://graph.facebook.com/me/photos?access_token=
。在这种情况下,me
始终是登录并为您的应用程序提供令牌的用户。在 PHP 中,假设您已将访问令牌存储在
$SESSION['token']
中,您可以使用以下方式发出照片负载请求:$photos
对象将是Facebook 文档<中描述的Photo
实体列表/a>.客户端
您需要在网页上设置 JavaScript SDK,如此处。
客户端的身份验证由 JavaScript SDK 处理,再次记录在身份验证指南中。
使用 SDK,您可以对 Graph API 进行客户端 JavaScript 调用以获取相同的照片结构:
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 theme
, 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:The
$photos
object will be a list ofPhoto
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:
检查堆栈上的另一个问题如何从 Facebook 导入照片?
Check this other question on stack How to import photos from Facebook?
我认为现在 Facebook 已经改变了导入照片的方式,所以我们必须首先获取相册,而不是导入该相册的照片。至少我是这样做到的。 :) 下面是使用 PHP 获取相册的基本 api 调用
:-
api('/me/albums', $params); ?>
获取相册照片 :-
api('/' . $album_id . '/photos', $params); ?>
现在这是完整的代码摘要。请将此代码复制到文件中并检查是否执行导入照片
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