Instantly share code, notes, and snippets.

Embed
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: https://github.com/facebook/php-sdk
<?php
// http://www.w3schools.com/php/php_sessions.asp
// 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_start();
$_SESSION['started'] = true;
}
?>
<!DOCTYPE html> <!--declares the document type as HTML5-->
<html lang="en-us">
<head>
<title>The Like Collector</title>
</head>
<body>
<?php
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>
<?php
// 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"/>
</form>
<!--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='http://stefanieduguay.com/?page_id=567' target='_blank'> research page</a>.
</p>
<!--Data handling info to adhere to FB's application policies: https://developers.facebook.com/policy-->
<?php
}
?>
<?php
// using FB php sdk reference: https://developers.facebook.com/docs/reference/php/facebook-api/
// used OAuth code example from the Faebook php sdk reference
require_once("php-sdk/facebook.php"); // downloaded from https://github.com/facebook/facebook-php-sdk
// begin OAuth using appID and app secret provided by Facebook
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; // OAuth doesn't work without this http://stackoverflow.com/questions/7374223/
$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
// http://stackoverflow.com/questions/5256553/extended-permissions-on-facebook-graph-api-in-php
if(is_null($facebook->getUser())){
$params = array('req_perms' => 'user_likes');
header("Location:{$facebook->getLoginUrl($params)}");
exit;
}
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: https://developers.facebook.com/docs/reference/api/user/
// create file name with the user number and a unique timestamp to prevent users from
// overwriting existing files
// http://stackoverflow.com/questions/10300690/how-can-i-add-a-time-stamp-to-a-file-name-uploaded-with-file-before-the-file-e
// http://php.net/manual/en/function.time.php
$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>
<table>
<thead>
<tr><td>Name</td><td>Date</td></tr>
</thead>
<tbody>
<!--to add to the user experience, this sets up the page for the Likes to be printed on screen-->
<?php
// 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: http://stackoverflow.com/questions/1293896/php-array-printing-using-a-loop
$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
// http://php.net/manual/en/function.fputcsv.php
fputcsv($file_handle, array(
$user_profile['data'][$i]['name'],
$user_profile['data'][$i]['created_time'],
$user_profile['data'][$i]['category'],
$user_profile['data'][$i]['id'],
)
);
} 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(
$user_profile['data'][$i]['name'],
0,
$user_profile['data'][$i]['category'],
$user_profile['data'][$i]['id'],
)
);
}
}
echo '</tbody></table>';
// print the likes in a table on screen for the user
// after looping over, close the file
fclose($file_handle);
} 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>';
error_log($e->getType());
error_log($e->getMessage());
}
} 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 -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment