Access Facebook Photo Albums using PHP - v1.0

13 comments
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;
}

13 comments

facebook dosen't redirect back to site

demo link is suspended :-( http://demos.frnzzz.com/cgi-sys/suspendedpage.cgi i don't believe is this activity is legal or not ...

i don't know why the demo link is suspended here is the link: http://demos.frnzzz.com/cgi-sys/suspendedpage.cgi i don't know is this activity is legal or not why they have suspend the domain name... creating confusing .... :-(

http://demos.frnzzz.com/d/?f=fbAlbum/photos.zip
This link show
Not Found

The requested URL /fbAlbum/photos.zip was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

This comment has been removed by the author.

Hey this link is not working please reply fast its very urgent..

@Feroz, Download link has been updated.

Buddy its not asking for login id & password of Facebook .can share some steps for that . its humble request

@Feroz, Its asking for login. I think you are already logged in your Facebook account. Try on another browser.

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

2)

( ! ) Notice: Undefined variable: pageURL in C:\wamp\www\facebookAlbum\photos.php on line 85

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

We would love to hear from you...

back to top