How to import photos from facebook into a website using php? [duplicate]
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,
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
}
});
Check this other question on stack How to import photos from Facebook?
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>';
}
?>
精彩评论