Tweets Backup Script

02Sep

Tweets Backup Script

by Craig Mayhew on Sun 2nd Sep 2012 under Code
So I wanted to backup and archive my tweets. I threw together this quick and dirty script to retrieve the last 200 tweets of a twitter user (In this case, me).

Latest source code available here:  https://github.com/craigmayhew/scratchpad/blob/master/php/retrieveTweets.php

My archived tweets: craig.mayhew.io/tweets/

<?php

define('DB_NAME', 'dbname');    // The name of the database
define('DB_USER', 'dbuser');     // Your MySQL username
define('DB_PASSWORD', 'agoodpassword'); // ...and password
define('DB_HOST', 'localhost');    // 99% chance you won't need to change this value

//connect to the database
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

//retrieve a twitter feed
$jsonFeed = file_get_contents('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=craigmayhew&count=200');
$json = json_decode($jsonFeed);

//input each of the tweets into a database table
foreach($json as $js){
$mysqli->query('INSERT IGNORE INTO tweets (time,text) VALUES (\''.$mysqli->real_escape_string(date('Y-m-d H:i:s',strtotime($js->created_at))).'\',\''.$mysqli->real_escape_string($js->text).'\')');
}
?>

PHP   Tweets   Script   Twitter  




© 2018 Craig Mayhew