A way to check if data exists? Using PHP

#1 - Aug. 4, 2011, 1:40 a.m.
Blizzard Post
I am pulling a listing of my guilds roster and storing it in our local database, and plan on updating it on a nightly basis to update character information. The issue I have is there are people on the guild roster, that doesn't have a character file to pull because they haven't logged in in years. When I go to retrieve character information, the script stops because it gets to a character who doesn't have information.

So for example, I use to following to get all the members of the guild:

$request = 'http://us.battle.net/api/wow/guild/Elune/SOK?fields=members';

// Make the request
$json = file_get_contents($request);
$json = json_decode($json, true);


I then need to get achievementPoints for each member, which while there is a line items for it in that api call, they are all set to 0, so I then do a call on the character itself to get that information using the following:

function getAchievement($character, $server) {
//Get Roster Data
$request = "http://us.battle.net/api/wow/character/" . $server . "/" . $character;

// Make the request
$json = file_get_contents($request);
$json = json_decode($json, true);
return $json["achievementPoints"];
}


The issue is, as mentioned, some people don't have character information because they haven't logged in a while. For example, Surefoot is listed in the guild roster, but, there is no
http://us.battle.net/api/wow/character/Elune/Surefoot, so I get a bunch of error message similar to this: file_get_contents(http://us.battle.net/api/wow/character/Elune/Surefoot) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I am wondering if there is a way to check that it would return something first, similar to php's file_exists as to not get all those error messages. I am using PHP, our server is on version 5.1. Any help or suggestions are welcome. Thanks!
Forum Avatar
Web & Mobile Team
#3 - Aug. 4, 2011, 2:14 a.m.
Blizzard Post
This is definitely a case where you want to store and track the last modified header and use it in subsequent requests. As the first pull will fetch the entire guild, later requests for characters that haven't updated or changed that use the last modified header will return a cached response and allow you to make additional calls toward your daily limit.