Tuesday, 8 October 2013

PHP - Facebook Photo Album Integrate with PHP

Step 1: Create a CURL file to pull Albums and respective Photos from Facebook.

albumsCurl.php

<?php
class FacebookPhotoAlbum {
    private $URL;
    private $TOKEN;
    private $PAGE;
    private $PHOTOS;
    private function setPage() {
            $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
            $this->PAGE = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    }

    private function setAlbumUrl($id) {
        if(is_numeric($id)) {
            if($this->TOKEN) {
                $this->URL = "http://graph.facebook.com/".$id."/albums?".$this->TOKEN;
                return true;
            } else {
                $this->URL = "http://graph.facebook.com/".$id."/albums";
                return true;               
            }
        } else {
            return false;
        }
    }
    private function setToken($appId, $appSecret) {
        $this->TOKEN = $this->curlGetFile('https://graph.facebook.com/oauth/access_token?type=client_cred&client_id='.$appId.'&client_secret='.$appSecret);
    }
       
    public function FacebookPhotoAlbum($id, $albumId, $aurl, $appId, $appSecret, $ALBUMNAME)
    {
        $this->setPage();
        if($id) {
            if($appId && $appSecret)
                $this->setToken($appId, $appSecret);
            $this->setAlbumUrl($id);
            if($albumId && $this->albumChk($albumId)) {
                $this->PHOTOS = 'http://graph.facebook.com/'.$albumId.'/photos';
                $json  = json_decode($this->curlGetFile($this->PHOTOS));
                include_once("photosdisplay.php");
                return true;
            } else if ($aurl) {
                $this->PHOTOS = urldecode($aurl);
                $json  = json_decode($this->curlGetFile($this->PHOTOS));
                if($json -> error) die("THERE HAS BEEN AN ERROR: album url invalid");
                foreach($json->data as $v) {
                    echo "<a class='ImageLink' href = '".$v->source."'><img width='110px' src='".$v->picture."' /></a>";
                }
                return true;
            } else {
                $albumsObj = json_decode($this->curlGetFile($this->URL));
                if($albumsObj -> error) die("THERE HAS BEEN AN ERROR: pageId invalid");
                include_once("albumsdisplay.php");
                return true;
            }
        }
        return false;
    }
    public function curlGetFile($curlUrl) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $curlUrl);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
       
    public function albumChk($albumId) {
        $json = json_decode($this->curlGetFile($this->URL));
        $arrayId = array();
        foreach($json->data as $v)
                    array_push($arrayId, $v->id);
        if(!in_array($albumId,$arrayId)) return false;
        return true;
    }
};
?>


Step 2: Create photosdisplay.php file to theme Album related Photos

<?php
//echo "<pre>";print_r($json);exit;
if($json -> error) die("THERE HAS BEEN AN ERROR:album id invalid");
if(count($json->data)) { ?>
    <div class="wrapper">
        <div class="formtitle" style="width:100%; height:14px; ">
           <div>Album Name: <?php print $ALBUMNAME;?></div>
        </div>
    </div>
    <?php foreach($json->data as $v) {?>
        <div class="wrapper photoind" style="border:1px solid #CCC; padding-bottom:10px; width:596px !important;">
            <div class="input" style="width:auto;">
                <div class="fb-post" data-href="https://www.facebook.com/photo.php?fbid=<?php print $v->id;?>&set=a.<?php print $albumId;?>.1073741825.<?php print $v->from->id;?>&type=1" data-width="550">
                <div class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/photo.php?fbid=<?php print $v->id;?>&set=a.<?php print $albumId;?>.1073741825.<?php print $v->from->id;?>&type=1">Post</a></div></div>
            </div>
          
            <?php if(count($v->comments->data)) { ?>
                <div style="padding-left: 20px; font-family: arial;font-size: 14px; ">
                <div><b><u>COMMENTS:</u></b></div><br>
                <ul>
                <?php foreach($v->comments->data as $eachComment) { ?>
                    <li><?php print $eachComment->message; ?> By <?php print $eachComment->from->name; ?></li>
                <?php } ?>
                </ul>
                </div>
            <?php } ?>
        </div>  
    <?php }
} ?>



Step 3: Create albumsdisplay.php file to theme Albums related Facebook Page

<div class="wrapper">
    <div class="formtitle" style="width:100%; height:14px; ">
        <div style="float:left;">Facebook Photo Albums</div>
       
    </div>
</div>
<?php
//echo "<pre>";print_r($albumsObj);exit;
foreach($albumsObj->data as $albums) {?>
    <div class="wrapper photoind">
        <div class="formtitle" style="width:auto;"><?php print strtoupper($albums->name);?></div>
        <div class="input" style="width:auto;">
            <a href="?aid=<?php print $albums->id;?>">
                <img src='https://graph.facebook.com/<?php print $albums->id; ?>/picture' />
            </a>
        </div>
       
        <div class="input" style="width:auto;">
            <p><?php print $albums->description; ?></p>
        </div>
    </div>   
<?php } ?>


Step 4: Create index.php to Call Facebook Photo Album Class based on APP ID & Page ID

<?php
$APP_ID = ""; // Get it from your facebook account
$PAGE_ID = ""; // Create a Page in your Facebook Account and paste that page id
 $APP_SECRET = ""; // Get it from your facebook account
?>

<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<title>Facebook Photo Albums</title>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=125456451798";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</head>

<body>
<div id ="wrapper">
    <?php $photoContent = new FacebookPhotoAlbum($PAGE_ID, $ALBUMID, $ALBUMURL, $APP_ID, $APP_SECRET, $ALBUMNAME);     ?>
</div>
</body>
</html>


1) Keep all these files in a folder and execute index.php

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.