Skip to content

Instantly share code, notes, and snippets.

@nporteschaikin
Last active December 11, 2015 03:38
Show Gist options
  • Select an option

  • Save nporteschaikin/4539098 to your computer and use it in GitHub Desktop.

Select an option

Save nporteschaikin/4539098 to your computer and use it in GitHub Desktop.
PHP code to grab and cache Twitter
<?php
function twitter ( $username, $file = 'twitter.xml', $interval = 600 ) {
$url = 'https://api.twitter.com/1/statuses/user_timeline/' . $username . '.xml?count=3';
if ( ( is_file ( $file ) && ( time () - filemtime ( $file ) ) > $interval ) || filesize ( $file ) == 0 ) {
$curl = curl_init();
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_URL, $url );
$xml = curl_exec ($curl);
curl_close ( $curl );
if ( strlen ( $xml ) > 200 ) {
file_put_contents ( $file, $xml );
}
}
if ( filesize ( $file ) > 0 ) {
return simplexml_load_file ( $file )->status;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment