Skip to content

Instantly share code, notes, and snippets.

@wgriffioen
Created January 24, 2012 22:09
Show Gist options
  • Save wgriffioen/1673025 to your computer and use it in GitHub Desktop.
Save wgriffioen/1673025 to your computer and use it in GitHub Desktop.
Twitter API voorbeeld met cache
<?php
define('TWITTER_USERNAME', 'dev10');
define('CACHE_FILE', dirname(__FILE__) . '/cache.txt');
if (!file_exists(CACHE_FILE) || filemtime(CACHE_FILE) < time() - 60 * 60) {
// Bestand bestaat niet of is te oud
// Als het te oud is verwijderen
if (file_exists(CACHE_FILE)) {
unlink(CACHE_FILE);
}
// Hele reut ophalen bij Twitter vandaan
$json = file_get_contents('http://twitter.com/status/user_timeline/' . TWITTER_USERNAME . '.json?count=10');
$tweets = json_decode($json);
// Wegschrijven naar cache bestand
$handler = fopen(CACHE_FILE, 'w');
foreach ($tweets as $tweet) {
fwrite($handler, $tweet->text . "\n");
}
fclose($handler);
}
// Inhoud van het bestand weergeven
echo file_get_contents(CACHE_FILE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment