Access Facebook Photo Albums using PHP – v1.0

I think it will be great if you can access your Facebook photo albums on your website. I have created an easy way to access your Facebook albums and photos on your site. Like the previous tutorial on ‘How to upload photos on Facebook using PHP’, this tutorial also requires the appId and secret code of your Facebook application. I will release updates of this post in near future The new version will include the comments posting on the photos.
Lets see it working…

Live Demo Download Script

PHP Code:

<?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>
CSS Code:
body{
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.alb {
margin: auto;
width:200px;
float:right;
padding:20px;
}
.alb p{
background-color: #EDEFF4;
}
.alb p.hd{
background-color: #EDEFF4;
color: #1C2A47;
font-size: 16px;
font-weight:bold;
padding:5px;
}
.alb p a{
color: #3B5998;
cursor: pointer;
text-decoration: none;
font-size: 11px;
padding:10px;
line-height: 25px;
}
.slideshow{
margin: auto;
float:left;
padding:20px;
}
.slideshow img {
padding: 15px;
border: 1px solid #ccc;
background-color: #eee;
}

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

13 thoughts on “Access Facebook Photo Albums using PHP – v1.0

  1. Hey dude thanks a lot .its working.I appreciate your help..
    just showing 2 warnings as
    1)
    ( ! ) Notice: Undefined index: action in C:wampwwwfacebookAlbumphotos.php on line 67

    2)

    ( ! ) Notice: Undefined variable: pageURL in C:wampwwwfacebookAlbumphotos.php on line 85

  2. An active access token must be used to query information about the current user.
    Please try clearing your browser cookies or click here.

Comments are closed.