-
-
Save jimbohne/5933586 to your computer and use it in GitHub Desktop.
Use Twitter API to export data (in CSV format) on people that are following you. See Installation notes for steps. Prerequisites: 1) Twitter OAuth PHP library 2) Create your own Twitter application and OAuth keys/tokens.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* followers.php - use Twitter API to export data (CSV format) on people that are following you | |
Installation: | |
1. Install Twitter OAuth PHP library (https://github.com/abraham/twitteroauth) | |
2. Adjust twitteroauth.php include path below | |
3. Create Twitter application (https://dev.twitter.com/apps) | |
4. Fill in 4 Twitter app keys below | |
5. Adjust $fields array if you want different fields saved | |
Usage: | |
php followers.php > followers.csv | |
Modified by: | |
Jim Bohne | |
Based on friends.php by: | |
Brian Cantoni | |
<brian AT cantoni DOT org> | |
http://www.cantoni.org | |
*/ | |
include ('./code/twitteroauth/twitteroauth.php'); | |
/* global settings */ | |
ini_set ('display_errors', 1); | |
error_reporting (E_ALL & ~E_NOTICE); | |
date_default_timezone_set ('America/New_York'); | |
/* Twitter user fields to write out as CSV */ | |
$fields = array ("name","screen_name","statuses_count","favourites_count","followers_count","friends_count","location","url"); | |
/* Twitter app keys */ | |
define ('CONSUMER_KEY', 'your-value-here'); | |
define ('CONSUMER_SECRET', 'your-value-here'); | |
define ('OAUTH_TOKEN', 'your-value-here'); | |
define ('OAUTH_TOKEN_SECRET', 'your-value-here'); | |
/* create Twitter object, override to use API v1.1 */ | |
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET); | |
if (!is_object($twitter)) { | |
fwrite (STDERR, "Error creating TwitterOAuth object\n"); | |
exit (-1); | |
} | |
$twitter->host = "https://api.twitter.com/1.1/"; | |
/* main loop: fetch follower ids, then details, then write out as CSV */ | |
fputcsv (STDOUT, $fields); | |
$cursor = -1; // first page | |
$follower_total = 0; | |
while ($cursor != 0) { | |
$params = array( | |
'stringify_ids' => true, | |
'count' => 100, | |
'cursor' => $cursor, | |
); | |
/* pull follower ID numbers, 100 at a time | |
docs: https://dev.twitter.com/docs/api/1.1/get/followers/ids | |
*/ | |
$followers = $twitter->get("followers/ids", $params); | |
if (!is_object($followers) || isset($followers->errors)) { | |
fwrite (STDERR, "Error retrieving followers\n"); | |
print_r($followers); | |
exit (-1); | |
} | |
$ids = implode (',', $followers->ids); | |
$cursor = $followers->next_cursor_str; | |
$follower_total += count($followers->ids); | |
fprintf (STDERR, "Found %d followers in this batch; cursor=%s\n", count($followers->ids), $cursor); | |
/* pull follower details, 100 at a time, using POST | |
docs: https://dev.twitter.com/docs/api/1.1/get/users/lookup | |
*/ | |
$params = array( | |
'user_id' => $ids, | |
); | |
$users = $twitter->post("users/lookup", $params); | |
if (!is_array($users)) { | |
fwrite (STDERR, "Error retrieving users\n"); | |
print_r ($users); | |
exit (-1); | |
} | |
foreach ($users as $u) { | |
$csv = array(); | |
foreach ($fields as $f) { | |
$csv[] = $u->{$f}; | |
} | |
fputcsv (STDOUT, $csv); | |
} | |
} | |
fprintf (STDERR, "Done! Found %d followers\n", $follower_total); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/username/public_html/tests/TwitterOAuthTest.php on line 10
Having this error during runtime.