Been fooling around a lot with Destiny API lately. It’s been an experiment in strangeness, to say the least. It has been a little difficult to work with, but only because at my office (where I do my thinking) the API Documentation is blocked (because Games are the devil at schools) which makes it tricky to figure out what data I need to provide in order to get the data I want (like having to provide a Platform ID and a Character ID in order to get number of kills).
That being said, it’s a learning exercise for objects and classes in PHP, which is fun. It’s certainly a good distraction from the clusterfuck that is American Politics today.
The endpoints are all listed here.
Important things to note: You need to supply a header of “X-API-Key” with an API Key that you get from Bungie’s Developer Site. You can get one here.
Other important tools: use hurl.it to make cURL requests via a web browser to test your API calls and see what happens with the data. You will need to set the header, which is an option hurl.it supports.
Here’s a snippet of some of my code, which will get published to GitHub once I am happy with it.
private function askAPI($query){
if(strlen($query)>0){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$query);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-API-Key: '.$this->apiKey));
$json = json_decode(curl_exec($ch));
if(gettype($json)=="NULL"){
// If we get no data, clearly there's something wrong.
throw new Exception("Fatal Error. Status Code A1: Bad Query For API.");
unset($json);
unset($ch);
}
$errorCode = $json->ErrorCode;
$errorStatus = $json->ErrorStatus;
$errorMessage = $json->Message;
if($errorCode>1){
throw new Exception(sprintf("Fatal Error. Status Code A2: Bad Destiny API Call, Error %s with Status %s, Message %s",$errorCode,$errorStatus,$errorMessage));
unset($json);
unset($ch);
}
else{
return($json);
unset($json);
unset($ch);
}
}
else{
throw new Exception("Fatal Error. Status Code A3: No Query For API.");
unset($json);
unset($ch);
}
}
Obviously there’s a lot going on here. Make a request, check the request, check the input to make sure it’s decent, all to make sure we actually get some sort of JSON out. Phew.
private function getMemberID($platform="PS",$account="VGATalon"){
if($platform=="PS"){
$platformid=2;
}
elseif($platform=="XBOX"){
$platformid=1;
}
else{
throw new Exception("Fatal Error. Status Code M1: Invalid / No Platform Specified.");
}
$query = sprintf("https://www.bungie.net/Platform/Destiny/SearchDestinyPlayer/%s/%s/",$platformid,$account);
try{
$data=$this->askAPI($query);
}
catch (Exception $e){
throw new Exception("Fatal Error. Status Code M2: ".$e);
}
try{
$membershipID = $data->Response[0]->membershipId;
}
catch (Exception $e){
throw new Exception(sprintf("Fatal Error. Status Code M3: Could Not Find User %s on Platform %s",$account,$platform));
}
return($membershipID);
}
This is another step of the process, where we need to get the membership ID of a person. This is based on which platform they use (Xbox or PS) and their character name. It’s a simple lookup. It returns the membership ID in a string format for us to use elsewhere.
There’s lots more to the process, including getting a character ID and then getting data about the character.
More of the examples will come to follow, as I continue developing the project.
I just wanted you to know that’s what the focus of my next bit of posts was going to be about.
I think I’m going to expand and make a Character an object instead of passing it around in terms of character IDs.
Food for thought.
-M, out.