I'm trying to fetch gamerscore data from gamercard.xbox.com with my little script:
test.php
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$regex = '/<div id=\"Gamerscore\">(.+?)<\/div>/';
$gamertag = 'Stallion83';
try {
$URL = file_get_contents('http://gamercard.xbox.com/en-US/' . $gamertag . '.card');
if ($URL == false) {
echo 'Error!';
}
} catch (Exception $e) {
echo $e;
}
preg_match($regex, $URL, $gs);
// Extract integer value from string
$gamerscore = filter_var($gs[1], FILTER_SANITIZE_NUMBER_INT);
// Force gs_int to be integer so it can be used with number_format later
$gs_int = (int)$gamerscore;
$textFile = 'data/gamerscore_' . $gamertag . '.txt';
// Save gamerscore value into everyone's own txt file
file_put_contents($textFile, $gs_int);
?>
Now this works and it creates a .txt file in the data folder which has only the gamerscore number inside. But my problem is if I run the script again after the gamerscore value has increased the script doesn't give me any errors and it seems to execute just fine but the gamerscore value it saves into the .txt file is the old value.
I can go to the URL http://gamercard.xbox.com/en-US/Stallion83.card and see the number is different than my script shows.
I thought it might be a caching issue but I think file_get_contents doesn't use caching.
Is there anything else I could set for file_get_contents to force it to get the most recent content of the URL specified? I tried using timeout but it didn't make any difference.
This is most likelly caused by cache. In this case, the server seems to be returning a cached version of the page.
Often, adding a random value to the URL can be a workaround, such as ?foo
.
So, in your case, something like:
[...] . $gamertag . 'card?' . mt_rand());