Last active
December 11, 2015 03:38
-
-
Save nporteschaikin/4539098 to your computer and use it in GitHub Desktop.
PHP code to grab and cache Twitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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