Twitter Export Script
Adam Franco October 13th, 2008
I have been using Twitter as a log of my daily doings and wished to export my time-line for reformatting into a calender format. Unfortunately TweetDumpr just retrieves the list of Tweets using a single fetch request which is limited by the Twitter API to a maximum of 200 Tweets. (Update: apparently TweetDumpr can get more than 200 Tweets. It just didn’t say so in its description.)
I wanted to export all 600+ of my tweets, so I wrote the following little php script to accomplish this. I have not yet tested it with many concurrent users or added a form to select which user to update. Until I do so, I won’t be providing it as an end-user service. You are free to put it on your own machine and use it though.
TwitterExport.php
<?php
/**
* This script will allow the export of complete user time-lines from the twitter
* service. It joins together all pages of status updates into one large XML block
* that can then be reformatted/processed with other tools.
*
* @since 10/13/08
*
* @copyright Copyright © 2008, Adam Franco
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*/
$user = 'afranco_work'; // Replace this with your user name.
header('Content-type: text/plain');
$allDoc = new DOMDocument;
$root = $allDoc->appendChild($allDoc->createElement('statuses'));
$root->setAttribute('type', 'array');
$page = 1;
do {
$numStatus = 0;
$pageDoc = new DOMDocument;
$res = @$pageDoc->load('http://twitter.com/statuses/user_timeline/'.$user.'.xml?page='.$page);
if (!$res) {
print "\n\n**** Error loading page $page ****";
exit;
}
foreach ($pageDoc->getElementsByTagName('status') as $status) {
$root->appendChild($allDoc->createTextNode("\n"));
$root->appendChild($allDoc->importNode($status, true));
$numStatus++;
}
print "\nLoaded page $page with $numStatus status updates.";
flush();
$page ++;
sleep(1);
} while ($numStatus);
print "\nDone loading timeline.";
print "\n\n\n";
$root->appendChild($allDoc->createTextNode("\n"));
print $allDoc->saveXml();
Usage (assuming PHP is installed)
- Save the code above on your machine as twitter_export.php
- Edit the code to change the
$uservariable to be your own Twitter username - From the command line run
php twitter_export.php - Copy/paste the XML output into a file for safe keeping and further processing

