Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Last active December 15, 2015 06:19
Show Gist options
  • Save jongacnik/5215416 to your computer and use it in GitHub Desktop.
Save jongacnik/5215416 to your computer and use it in GitHub Desktop.
Little snippet to grab tweets and store the json at maximum once an hour to avoid going over api limits. Does all this without cron so we use the log file to keep track of the last grab and the tweets.json stores the tweets for us.
<?php
$handle = 'handle';
$count = 24;
$url = 'http://api.twitter.com/1/statuses/user_timeline/'.$handle.'.json?count='.$count;
$lastGrabLog = './lastGrab.log';
$twitGit = './tweets.json';
$interval = 3600;
if (file_exists($lastGrabLog)) {
$lastGrab = file_get_contents($lastGrabLog);
if (time() - $lastGrab >= $interval) {
$tweets = file_get_contents($url);
file_put_contents($twitGit, $tweets);
file_put_contents($lastGrabLog, time());
}
}
?>
[
{ "tweet":"tweet" }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment