Last active
July 12, 2022 21:46
-
-
Save bcantoni/5457114 to your computer and use it in GitHub Desktop.
Use Twitter API to export data (in CSV format) on people you are following. 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 | |
/* friends.php - use Twitter API to export data (CSV format) on people you are following | |
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 friends.php > /tmp/friends.csv | |
Written 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 friend ids, then details, then write out as CSV */ | |
fputcsv (STDOUT, $fields); | |
$cursor = -1; // first page | |
$friend_total = 0; | |
while ($cursor != 0) { | |
$params = array( | |
'stringify_ids' => true, | |
'count' => 100, | |
'cursor' => $cursor, | |
); | |
/* pull friend ID numbers, 100 at a time | |
docs: https://dev.twitter.com/docs/api/1.1/get/friends/ids | |
*/ | |
$friends = $twitter->get("friends/ids", $params); | |
if (!is_object($friends) || isset($friends->errors)) { | |
fwrite (STDERR, "Error retrieving friends: " . print_r ($friends, 1) . "\n"); | |
exit (-1); | |
} | |
$ids = implode (',', $friends->ids); | |
$cursor = $friends->next_cursor_str; | |
$friend_total += count($friends->ids); | |
fprintf (STDERR, "Found %d friends in this batch; cursor=%s\n", count($friends->ids), $cursor); | |
/* pull friend 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: " . print_r ($users, 1) . "\n"); | |
exit (-1); | |
} | |
foreach ($users as $u) { | |
$csv = array(); | |
foreach ($fields as $f) { | |
$csv[] = $u->{$f}; | |
} | |
fputcsv (STDOUT, $csv); | |
} | |
// try to avoid being rate limited | |
sleep (2); | |
} | |
fprintf (STDERR, "Done! Found %d friends\n", $friend_total); |
I am having trouble executing this. I am new to PHP hence trying to learn. I get the following error:
Warning: fputcsv() expects parameter 1 to be resource, string given in E:\xampp\htdocs\following.php on line 46
Warning: fprintf() expects parameter 1 to be resource, string given in E:\xampp\htdocs\following.php on line 68
Warning: print_r() expects parameter 2 to be boolean, array given in E:\xampp\htdocs\following.php on line 87
Please help me resolve this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also check out https://gist.github.com/jimbohne/5933586 for a modified version of this script to find your friends (i.e. those following you).