├── README.md ├── index.php └── cache.php /README.md: -------------------------------------------------------------------------------- 1 | Display Xbox live data with xboxapi and cache it 2 | ========= 3 | 4 | Xbox does not provide an official API for anyone to use. Their are a few sites that provide an API one of which is https://xboxapi.com/v1 xboxapi makes it very easy to pull your Xbox API data. 5 | 6 | In this tutorial I'll show you how to display your Xbox Live data and then cache it, this will enable the page to load faster and avoid going over xboxapi's API Limit which is set to 350 requests an hour. 7 | 8 | These files acompany the tutorial: [Display Xbox live data with xboxapi and cache it](http://daveismyname.com/display-xbox-live-data-with-xboxapi-and-cache-it-bp) 9 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Xbox Live 6 | 7 | 8 | 9 | 10 |
11 | 12 |

Xbox Live Profile

13 | 14 |
15 |

Enter your xbox gamertag

16 |
17 |

Gamertag:

18 |

19 |
20 |
21 | 22 | 23 |
24 | get_data($gamertag, "https://xboxapi.com/v1/json/profile/$gamertag"); 29 | $obj = json_decode($json); 30 | 31 | if($obj->Success == 1){ 32 | echo 'API Hourly Limit: '.$obj->API_Limit.'
'; 33 | echo 'Gamertag: '.$obj->Player->Gamertag.'
'; 34 | echo 'Membership: '.$obj->Player->Status->Tier.'
'; 35 | 36 | if($obj->Player->Status->Online == 1){ 37 | echo 'Online
'; 38 | } else { 39 | echo 'Offline
'; 40 | } 41 | 42 | echo $obj->Player->Status->Online_Status.'
'; 43 | echo "
"; 44 | echo 'Gamerscore: '.$obj->Player->Gamerscore.'
'; 45 | echo 'Reputation: '.$obj->Player->Reputation.'
'; 46 | echo $obj->Player->Name.'
'; 47 | echo $obj->Player->Location.'
'; 48 | 49 | echo "

"; 50 | foreach($obj->RecentGames as $row){ 51 | echo "$row->Name "; 52 | } 53 | echo "

"; 54 | } else { 55 | echo $obj->Error; 56 | } 57 | ?> 58 |
59 | 60 | 61 |
62 | 63 | -------------------------------------------------------------------------------- /cache.php: -------------------------------------------------------------------------------- 1 | get_cache($label)){ 23 | return $data; 24 | } else { 25 | $data = $this->do_curl($url); 26 | $this->set_cache($label, $data); 27 | return $data; 28 | } 29 | } 30 | 31 | function set_cache($label, $data) 32 | { 33 | file_put_contents($this->cache_path . $this->safe_filename($label) .'.cache', $data); 34 | } 35 | 36 | function get_cache($label) 37 | { 38 | if($this->is_cached($label)){ 39 | $filename = $this->cache_path . $this->safe_filename($label) .'.cache'; 40 | return file_get_contents($filename); 41 | } 42 | 43 | return false; 44 | } 45 | 46 | function is_cached($label) 47 | { 48 | $filename = $this->cache_path . $this->safe_filename($label) .'.cache'; 49 | 50 | if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true; 51 | 52 | return false; 53 | } 54 | 55 | //Helper function for retrieving data from url 56 | function do_curl($url) 57 | { 58 | if(function_exists("curl_init")){ 59 | $ch = curl_init(); 60 | curl_setopt($ch, CURLOPT_URL, $url); 61 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 62 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100); 63 | $content = curl_exec($ch); 64 | curl_close($ch); 65 | return $content; 66 | } else { 67 | return file_get_contents($url); 68 | } 69 | } 70 | 71 | //Helper function to validate filenames 72 | function safe_filename($filename) 73 | { 74 | return preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($filename)); 75 | } 76 | } 77 | 78 | ?> --------------------------------------------------------------------------------