function boston_return_tweets($count = 1) { include_once locate_template('includes/twitter_api.php'); $username = boston_get_option('twitter-username'); $oauth_access_token = boston_get_option('twitter-oauth_access_token'); $oauth_access_token_secret = boston_get_option('twitter-oauth_access_token_secret'); $consumer_key = boston_get_option('twitter-consumer_key'); $consumer_secret = boston_get_option('twitter-consumer_secret'); if (!empty($username) && !empty($oauth_access_token) && !empty($oauth_access_token_secret) && !empty($consumer_key) && !empty($consumer_secret)) { $cache_file = dirname(__FILE__) . '/includes/' . 'twitter-cache.txt'; if (!file_exists($cache_file)) { file_put_contents($cache_file, ''); } $modified = filemtime($cache_file); $now = time(); $interval = 600; // ten minutes $response = json_decode(file_get_contents($cache_file), true); if (!$modified || empty($response) || $now - $modified > $interval || !empty($response['errors']) || !empty($response['error'])) { $settings = array('oauth_access_token' => $oauth_access_token, 'oauth_access_token_secret' => $oauth_access_token_secret, 'consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'username' => $username, 'tweets' => $count); $twitter = new TwitterAPIExchange($settings); $response = $twitter->get_tweets(); if ($response) { $cache_static = fopen($cache_file, 'w'); fwrite($cache_static, json_encode($response)); fclose($cache_static); } } } else { $response = array('error' => 'NOK'); } return $response; }