UrPics Code Walkthrough

From Facebook Developer Wiki

Jump to: navigation, search

[edit] Introduction

I know a lot of people are confused about how to do very simple tasks with the Facebook API, so I wrote up this walkthrough hoping that it will help at least some people out there who are just starting on their first Facebook Application. The full source code can be viewed here. You may also view and use the urPics -> myPics Photo Downloader application here.

[edit] Step-by-Step

  • 3) The application now creates a session with Facebook so that you can retrieve information. It does so using this code: $session = $facebook->do_get_session($_GET['auth_token']);
  • 4) This part got a little tricky and seemed to differ from the documentation in my case, but I then stored the session key and user ID in a session variable by doing this:
    • $_SESSION['session_key'] = $_COOKIE[$appapikey.'_session_key'];
    • $_SESSION['session_uid'] = $_COOKIE[$appapikey.'_user'];
  • 5) Once I have that information stored, I can then call any method I want from Facebook. The one which retrieves all of your tagged photos is: $pics = $facebook->api_client->photos_get($_SESSION['session_uid'],null,null); This took some experimentation, but the Facebook API Test Console really came in handy here.
  • 6) I filter the images by excluding ones you've uploaded. I simply do this by doing: if($pic['owner']!=$_SESSION['session_uid']) in the loop that outputs the images. In that same loop, I add the image URLs to a $_SESSION array for use later: $_SESSION['images'][$pic['pid']] = $pic['src_big'];
  • 7) When the user clicks the "Prepare my Download" link, an Ajax call is made that takes all of the image URLs that were stored in the $_SESSION array and downloads them to my server. I do this using the copy() function.
  • 8) Once all images are downloaded, they are immediately placed into a .zip file using exec('zip -n jpg fbpictures *long file list outputted from the array*');. As soon as exec() finishes, the photos are deleted, leaving only the .zip file in the directory.
  • 9) After all of this finishes, the download link for the .zip file is made visible to the user. This link goes to getdl.php.
  • 10) In getdl.php, the .zip file is retrieved based on your Facebook user ID, which was stored in a session variable eariler ($_SESSION['session_uid']). Once the file finishes downloading, it is immediately deleted in order to save space on my server, and to protect your privacy.

[edit] How the Friends List Works

Since facebook.friends.get returns the user ID's of your friends, this list must be parsed and one more API call must be made in order to retrieve their whole names. In order to save time and bandwidth, the first time this function is called, it saves the friend list to a session array, which then gets called each subsequent time instead of making the 2 API calls again. Here's the code that handles this and outputs it to a dropdown list:

function getFriendsList()
	{
		GLOBAL $facebook;
		
		echo "<option name=\"".$_SESSION['session_uid']."\" value=\"".$_SESSION['session_uid']."\" >Your Photos</option>\n";
		
		if(!isset($_SESSION['friends']))
		{
			$userids = $facebook->api_client->friends_get();
			$names = $facebook->api_client->users_getInfo($userids, array("name"));
			$i = 0;
			foreach($names as $name)
			{
				
				echo "<option name=\"".$name['uid']."\" value=\"".$name['uid']."\" >".$name['name']."</option>\n";
				$_SESSION['friends'][$i]['name'] = $name['name'];
				$_SESSION['friends'][$i]['uid'] = $name['uid'];
				$i++;
			}
		}
		else
		{
			foreach($_SESSION['friends'] as $friend)
			{
				echo "<option name=\"".$friend['uid']."\" value=\"".$friend['uid']."\" >".$friend['name']."</option>\n";
			}
		}
	}

reference