Skip to content

Instantly share code, notes, and snippets.

Created Apr 18, 2013
What would you like to do?
This is a Facebook application created for a class project. It downloads a user's Facebook Likes. To use it, you must create a directory folder for the csv files and you must have the Facebook PHP SDK:
// check to see if a session exists for the user.
// if not create one. I use the session to keep track
// of which part of the app the user should see
if (!isset($_SESSION)){
$_SESSION['started'] = true;
<!DOCTYPE html> <!--declares the document type as HTML5-->
<html lang="en-us">
<title>The Like Collector</title>
if (isset($_POST) && array_key_exists('number', $_POST) && $_POST['number'] != "") {
// when the form has been submitted with a number that is not an empty string
// add the number to the session variable and show the next screen
$_SESSION['number'] = $_POST['number'];
<div id="container" style="width: 720px; margin: 0 auto;">
<h2> <b> Research Study: Indications of LGBTQ Identity on Facebook</b></h2>
// if the user does not have a number on their session
// this is the first screen they should see with the Welcome text
if (!array_key_exists('number', $_SESSION)) {
<p> Thank you for participating in the study "Indications of LGBTQ
Identity on Facebook". This is the second phase in the study after completion of the online survey. </p>
<p> After entering the ID number provided to you in the study e-mail, the Like Collector
application will request that you login to Facebook. It will then collect a list of your page "Likes"
and store them in a file on a secure server where they will be looked at for the purpose of the study.
Thank you again for your participation! </p>
<form action="#" method="POST">
<label for="number">ID Number:</label>
<input name="number" id="number" type="text" />
<input value="Go" type="submit"/>
<!--this is the box where the user will put the ID number, also the session variable-->
<p style="font-size: 0.8em; color: #888;">
Privacy policy and conditions: The Like Collector application has been created for the purpose of this study
and this is its sole intended use. The application collects participants' Facebook likes and the following information
about each page like: the date on which the user subscribed to the page, the page name, the category, and the page ID.
All user information will be stored on a secure server. Participants are only asked to run the application after
completing a study consent form and authorizing the Facebook application. For more information about the study,
please visit the <a href='' target='_blank'> research page</a>.
<!--Data handling info to adhere to FB's application policies:>
// using FB php sdk reference:
// used OAuth code example from the Faebook php sdk reference
require_once("php-sdk/facebook.php"); // downloaded from
// begin OAuth using appID and app secret provided by Facebook
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; // OAuth doesn't work without this
$config = array(
'appId' => '506354189427519',
'secret' => '98e66a9382f2c42d9920b267e8c1060a',
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
// User Likes are public by default but the app requests permission
// in case users have changed their privacy settings
$params = array('req_perms' => 'user_likes');
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me/likes','GET');
// queries for user Likes
// referred to Graph API info for query term:
// create file name with the user number and a unique timestamp to prevent users from
// overwriting existing files
$file_handle = fopen('./saved_likes/'.time().'_likes_for_'.$_SESSION['number'].'.csv', 'w');
//put the column names for the csv
fputcsv($file_handle, array("name","date", "category", "id"));
<h3>These are your likes:</h3>
<!--to add to the user experience, this sets up the page for the Likes to be printed on screen-->
// loop over the array returned by the user_likes query
// used PHP textbook "PHP6 and MySQL" mentioned in paper references to learn 'for' loop
// also used:
$length = count($user_profile['data']);
for ($i = 0; $i < $length; $i++) {
// check to see that the Like has a created time. I found that my earliest Likes
// did not have a time and would cause errors
// created an "if...else" statement to handle this
if (array_key_exists('created_time',$user_profile['data'][$i]) == true) {
// if the created time exists print it on the screen with the name
print '<tr><td>'.$user_profile['data'][$i]['name'].'</td><td>'.$user_profile['data'][$i]['created_time'].'</td></tr>';
// and write this information from the array to the csv file
fputcsv($file_handle, array(
} else {
// else indicate that the date was not available when printing the like name
print '<tr><td>'.$user_profile['data'][$i]['name'].'</td><td>N/A</td></tr>';
// put a zero in the csv column for the date when recording the name
fputcsv($file_handle, array(
echo '</tbody></table>';
// print the likes in a table on screen for the user
// after looping over, close the file
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';
} elseif (array_key_exists('number', $_SESSION)) {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login</a> to Facebook';
</div> <!-- end of #container -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment