Main Menu

Get pictures of a specific facebook album to your website

Get pictures of a specific facebook album to your website

If you want to import spcific images from a facebook album (note that the album must have the privacy settings set to public), you can simply use Facebooks OpenGraph. 

You will first need to import Facebooks PHP SDK to you site, which can be found at Github, you actually need the src/Facebook folder and the autoload.php. 

After you have it setup in your webservers directory, you can jump straigt to the page where you would like to load the pictures. 

Add and point to the SDK which you have loaded on the server, and request the classes that you would require. 

Note that you would already need to have created a Facebook app from facebook developers

                        /* PHP SDK v4.0.0 */
			// Skip these two lines if you're using Composer
			define('FACEBOOK_SDK_V4_SRC_DIR', 'point/to/your/facebookSDK/directory/src/Facebook/');
			require 'point/to/your/facebookSDK/directory/autoload.php';
			
			use Facebook\FacebookSession;
			use Facebook\FacebookRequest;
			use Facebook\GraphUser;
			use Facebook\GraphObject;
			use Facebook\FacebookRequestException;
			
			FacebookSession::setDefaultApplication('{your app id}','{your app secret}');
			
			// Use one of the helper classes to get a FacebookSession object.
			//   FacebookRedirectLoginHelper
			//   FacebookCanvasLoginHelper
			//   FacebookJavaScriptLoginHelper
			// or create a FacebookSession with a valid access token:
			
			$session = new FacebookSession('{your token}');

Once that is up and running, it is now time to get the id of the album that you are going to use to get the pictures from.

There are a few ways you can do this, but the easiest way is to actually visit the album on facebook and get the ID from the URL, which is the number between the first two dots.

albumID.png

Now that that is done, we go straigh into makeing a graph call

 

 
                       /* make the API call */
			$request = new FacebookRequest(
			  $session,
			  'GET',
			  '/{youralbumID}/photos?fields=picture&limit=5'
			);
			 
			/* handle the result */
			$response = $request->execute();
			$graphObject = $response->getGraphObject(); 
			$graphObject = $graphObject->getPropertyAsArray('data');
		 

What I am doing here is specifin the album ID, saying that I want the photos from there, with the picture field and limiting it to 5 pictures. 

Next a loop that will dump your results into an img tage that it can display them to the users that are visting your page.

 
                           foreach($graphObject as $picture) {
		 
			   echo '<img src="'.$picture-&gt;getProperty('picture').'" alt="" />;
			   }
Garen Meserlian About Author

Co-Founder of webtodesigns IT Director, front-end developer, photographer, designer, CISCO certified and I like cooking

comments powered by Disqus