Skip to content

Instantly share code, notes, and snippets.

@caseysoftware
Last active December 28, 2016 11:31
Show Gist options
  • Select an option

  • Save caseysoftware/93626136f3dff258051b to your computer and use it in GitHub Desktop.

Select an option

Save caseysoftware/93626136f3dff258051b to your computer and use it in GitHub Desktop.
This is a simple script to get all of your likes from Facebook via their API for my blog post: Social APIs for Social Evil - http://caseysoftware.com/blog/social-apis-for-social-evil
<?php
// This includes the PHP library
require_once __DIR__ . '/vendor/autoload.php';
/*
* This file has a total of three values:
* - The App ID and App Secret received when you create a new Application.
* - The person/application-specific Access Token, normally granted via the OAuth process.
*
* For simplicity here, I've skipped the OAuth process and retrieved my Access Token via the
* Graph API Explorer from the Developer Portal: https://developers.facebook.com
*/
include 'creds.php';
// This establishes the connection.
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.2',
]);
// This first block gets the user information. In this case, it will *only* be for you.
$response = $fb->get('/me?fields=id,name', $access_token);
$user = $response->getGraphUser();
$user_id = $user['id'];
/*
* Using the user_id from above, let's look at my likes.
*
* One of the things to be aware of is that Facebook uses cursor-based paginated results. It means that
* instead of getting one *long* list of whatever data, you're going to get a page with a URL pointing
* to the next page. You request the next page and repeat as long as there's still another page.
*/
$more_pages = true;
$cursor = '';
$all_likes = array();
while ($more_pages) {
$response = $fb->get($user_id . '/likes?after=' . $cursor, $access_token);
$likes = $response->getDecodedBody()['data'];
foreach ($likes as $_like) {
$all_likes[] = $_like;
}
$paging = $response->getDecodedBody()['paging'];
$cursor = $paging['cursors']['after'];
if (!array_key_exists('next', $paging)) {
$more_pages = false;
}
}
/*
* Now that we have my list of likes, we can score them using something like the AlchemyAPI to identify/filter terms
* or - more simply - we can whitelist or blacklist anything we want to start building out a profile.
*/
print_r($all_likes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment