Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function fetchInfo($gt) {
- $key = "********************************************";
- $file = file_get_contents("http://www.bungie.net/api/reach/reachapijson.svc/player/details/nostats/".$key."/".$gt);
- $json = json_decode($file, true);
- unset($file, $key);
- $cache[] = time();
- $cache[] = $json['Player']['armor_completion_percentage']; // Obvious
- $cache[] = $json['Player']['commendation_completion_percentage']; // Obvious
- $cache[] = $json['Player']['games_total']; // Games played
- $cache[] = $json['Player']['service_tag']; // Obvious
- $date = preg_match("#\/Date\((.+?)-(.+?)\)\/#i", $json['Player']['last_active'], $match);
- $cache[] = $match[1] / 1000;
- return $cache;
- }
- $gt = "halo2freeek";
- $cachefile = "./" . md5($gt) . "_cache.txt";
- $newCache = false;
- $newFile = false;
- if(file_exists($cachefile)) {
- // The cache file exists, we need to read it's contents and
- // Check if the timestamp is greater than one hour ago.
- $file = file_get_contents($cachefile);
- $cache = explode("#|#", $file);
- unset($file);
- $cacheTime = $cache[0];
- $timeDiff = time() - $cacheTime;
- if($timeDiff > 300) {
- // It has been more than an hour since this cache file was created
- // So we recreate it with updated information from the API
- echo "The cache file was older than 5 minutes, so we'll refresh it.<br><br>\n\n";
- $cache = fetchInfo($gt);
- $newCacheTime = $cache[0];
- $timeDiff = time() - $newCacheTime;
- $newCache = true;
- $str = implode("#|#", $cache);
- file_put_contents($cachefile, $str);
- unset($str);
- // We still have the $cache variable which has overwritten the
- // Original and now holds NEW information.
- } else {
- // It has been less than an hour, so we're going to just read the
- // Cache file's contents and use that information in our image
- echo "The cache file was younger than 5 minutes, so we'll use its contents.<br><br>\n\n";
- // We're just going to print the existing information now, for
- // Example purposes.
- }
- } else {
- // The cache file doesn't exist, so we'll need to grab the
- // Information from the API and create a cache file.
- echo "The cache file doesn't exist, so we'll create it.<br><br>\n\n";
- $cache = fetchInfo($gt);
- $newCacheTime = $cache[0];
- $timeDiff = time() - $newCacheTime;
- $newCache = true;
- $newFile = true;
- $str = implode("#|#", $cache);
- file_put_contents($cachefile, $str);
- unset($str);
- // We still have the $cache variable which has overwritten the
- // Original and now holds NEW information.
- }
- echo "<pre>";
- foreach($cache as $key => $inf) {
- switch($key) {
- case 0:
- echo "Cache Time: ";
- break;
- case 1:
- echo "Armory Completion: ";
- break;
- case 2:
- echo "Commendation Completion: ";
- break;
- case 3:
- echo "Games Played: ";
- break;
- case 4:
- echo "Service Tag: ";
- break;
- case 5:
- echo "Last Active: ";
- break;
- }
- if($key != 5) {
- echo $inf;
- } else {
- echo date($mydate, $inf);
- }
- echo "\n";
- }
- echo "</pre>Raw Array: <br><br>";
- echo "<pre>";
- print_r($cache);
- echo "</pre>";
- echo "The current time is <b>" . date($mydate) . "</b> (" . time() . ")<br>\n";
- echo "The previous cache was " . (($newFile) ? " <b>never</b>.<br>\n" : "on <b>" . date($mydate, $cacheTime) . "</b> (" . $cacheTime . ")<br>\n");
- echo (($newCache) ? "The most recent cache was <b>right now.</b><br>\n" : "");
- echo "The difference between those times is <b>" . $timeDiff . "</b> seconds.<br>\n";
- echo "The next cache will occur after the page has been refreshed in <b>" . (300 - $timeDiff) . "</b> seconds.";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement