Last active
November 1, 2022 02:44
-
-
Save levymetal/a32ab0dd8c699e1e5415 to your computer and use it in GitHub Desktop.
Custom version of my twitter cache that works with multiple usernames. Original here: http://christianvarga.com/how-to-get-public-feeds-using-twitters-v1-1-api/#comment-2121870201
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 | |
// call this script using a GET parameter, eg: http://your-domain.com/twitter-cache.php?screen_name=your_twitter_username | |
error_reporting( 0 ); // don't let any php errors ruin the feed | |
$username = $_GET['screen_name']; | |
$number_tweets = 4; | |
$feed = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={$username}&count={$number_tweets}&include_rts=1"; | |
$cache_file = dirname(__FILE__).'/cache/'.'twitter-cache-'.$username; | |
$modified = filemtime( $cache_file ); | |
$now = time(); | |
$interval = 600; // ten minutes | |
// check the cache file | |
if ( !$modified || ( ( $now - $modified ) > $interval ) ) { | |
$bearer = 'AAAAAAAAAAAAAAAAAAAAA...AAAAAA'; | |
$context = stream_context_create(array( | |
'http' => array( | |
'method'=>'GET', | |
'header'=>"Authorization: Bearer " . $bearer | |
) | |
)); | |
$json = file_get_contents( $feed, false, $context ); | |
if ( $json ) { | |
$cache_static = fopen( $cache_file, 'w' ); | |
fwrite( $cache_static, $json ); | |
fclose( $cache_static ); | |
} | |
} | |
header( 'Cache-Control: no-cache, must-revalidate' ); | |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); | |
header( 'Content-type: application/json' ); | |
$json = file_get_contents( $cache_file ); | |
echo $json; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment