Using PHP Display Facebook Albums and Photos on the Website

facebook photo album fetch using PHP

Facebook provide a well known API i.e graph API that previously called Social Graph API . It allow us to embed facebook album to our own website .
Using this Graph API and PHP you can easily create facebook photo album .
In this below tutorial we will guide you how to fetch facebook photo album to your website .

We will be creating a simple PHP scripts that allow you to fetch images from your facebook page .You can also customize as per your needs.
Once our scripts ready ,it will automatically fetch new images when you post on your facebook page .

To achieve this requirements, first of all you have to create facebook app and get APP ID and APP Secret key from there.
If you are new to creating facebook APP ,Follow below quick steps

  • Access Facebook Developers page and login to your Facebook developer account.
  • Navigate to the Apps page.
  • Click on Add a New App. A popup would be appear.
  • You would be redirected to the Apps Dashboard.
  • Click on Settings from the left side navigation menu panel.
  • Enter the App Domains (If you looking to text the app in your localhost host then type there ‘http://localhost’).
  • Click on Add Platform button and select Website as a platform.
  • Now enter your website URL (http://localhost/facebook-login/) and Save Changes.
  • Click on App Review link from left side navigation menu panel. Make your app live and available it for public.
  • Now you have successfully created your new facebook app.
  • Back to the Apps Dashboard, you’ll see the App ID and App Secret.

php mysql video tutorial in Hindi language by UnitedWebSoft

Fetch Photo Albums from Facebook using PHP

Here in our main index.php file ,we are going to fetch facebook photo album . You need to mention your newly created facebook APP ID ($app_id)), APP Secret($app_secret) also mention your facebbok page id ($facebook_page_id)


 * Get access token using Facebook Graph API
    // Get access token from session
    $access_token = $_SESSION['facebook_access_token'];
    // Facebook app id & app secret 
    $app_id = 'InsertAppID'; 
    $app_secret = 'InsertAppSecret';
    // Generate access token
    $fb_graph_act_link = "{$app_id}&client_secret={$app_secret}&grant_type=client_credentials";
    // Retrieve access token
    $accessTokenJson = file_get_contents($fb_graph_act_link);
    $accessTokenObj = json_decode($accessTokenJson);
    $access_token = $accessTokenObj->access_token;
    // Store access token in session
    $_SESSION['facebook_access_token'] = $access_token;

// Get photo albums of Facebook page using Facebook Graph API
$fields = "id,name,description,link,cover_photo,count";
$facebook_page_id = "Insert_Facebook_Page_ID";
$graph_album_link = "{$facebook_page_id}/albums?fields={$fields}&access_token={$access_token}";

$jsonData = file_get_contents($graph_album_link);
$fbAlbumObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);

// Facebook albums content
$fbAlbumData = $fbAlbumObj['data'];

Below we are using foreach loop to fetch facebook albums with cover photo ,name and description

// Render all photo albums
foreach($fbAlbumData as $data){
    $id = isset($data['id'])?$data['id']:'';
    $name = isset($data['name'])?$data['name']:'';
    $description = isset($data['description'])?$data['description']:'';
    $link = isset($data['link'])?$data['link']:'';
    $cover_photo_id = isset($data['cover_photo']['id'])?$data['cover_photo']['id']:'';
    $count = isset($data['count'])?$data['count']:'';
    $pictureLink = "photos.php?album_id={$id}&album_name={$name}";
    echo "<div class='fb-album'>";
    echo "<a href='{$pictureLink}'>";
    echo "<img src='{$cover_photo_id}/picture?access_token={$access_token}' alt=''>";
    echo "</a>";
    echo "<h3>{$name}</h3>";

    $photoCount = ($count > 1)?$count. 'Photos':$count. 'Photo';
    echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>";
    echo "<p>{$description}</p>";
    echo "</div>";

Above you can see a link that points to photos.php for showing all images of the respective album .

Retrieve Photos from Facebook Album using PHP

In this photos.php file ,w ill have to retrieve all photos from an album to display those facebook photos as photo gallery .

Initially get album_id, album_name using query string of the URL and access toen from the session .

Then retrieve all the photos of a particular album using Facebook Graph API.


// Get album id from url
$album_id = isset($_GET['album_id'])?$_GET['album_id']:header("Location: index.php");
$album_name = isset($_GET['album_name'])?$_GET['album_name']:header("Location: index.php");

// Get access token from session
$access_token = $_SESSION['facebook_access_token'];

// Get photos of Facebook page album using Facebook Graph API
$graphPhoLink = "{$album_id}/photos?fields=source,images,name&access_token={$access_token}";
$jsonData = file_get_contents($graphPhoLink);
$fbPhotoObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);

// Facebook photos content
$fbPhotoData = $fbPhotoObj['data'];

Below again using foreach loop to get the image path and name .

// Render all photos
foreach($fbPhotoData as $data){
    $imageData = end($data['images']);
    $imgSource = isset($imageData['source'])?$imageData['source']:'';
    $name = isset($data['name'])?$data['name']:'';
    echo "<div class='fb-album'>";
    echo "<img src='{$imgSource}' alt=''>";
    echo "<h3>{$name}</h3>";
    echo "</div>";

For Complete Facebook Graph API reference .visit offical facebook graph API reference page
If you have any query ,do write on below comments. I will try my best to reply soon.

  • Bhautik Gosai

    how to get facebook page id?

  • Suhag Lapani

    please provide this code