PHP Details

From Halo: Reach API Wiki
Jump to: navigation, search
This article is a stub. You can help improve the wiki by expanding it.

Contents

[edit] Requesting Data

In order to utilize the API, you must request data from it. PHP offers a couple of different methods to get this done.

[edit] File Get Contents

The most simplistic way to retrieve data from the API is with the "file_get_contents" method.

To use this method, you simply pass in the URL that you wish to retrieve, and the content will be returned as a string.

Here is an example to work from:

$gamertag = "Null Parameter";
$url = "http://www.bungie.net/api/reach/reachapijson.svc/file/share/{apikey}/".rawurlencode($gamertag);
$output = file_get_contents($url);

You can see the first two lines are constructing the URL to request. In the second line you'll notice the method "rawurlencode". This method is used to encode the gamertag and replace any spaces and other special characters with the appropriate URL equivalents. For example, after encoding the gamertag "Null Parameter", the result would appear as "Null%20Parameter".

NOTE: "Rawurlencode" is the recommended method to encode the player's gamertag. If for any reason you are unable to use this method, it is possible to replace spaces within the gamertag using the "str_replace" function. Please note that using "str_replace" is highly discouraged, as other special characters are likely to be encoded improperly. The following is an example that uses "str_replace":
$url = "http://www.bungie.net/api/reach/reachapijson.svc/file/share/{apikey}/".str_replace(' ', '%20', $gamertag);

The third line in the example is the code that actually retrieves the data from the API. The file_get_contents method queries the URL and returns the results in a string, simple as that.

[edit] cURL

"cURL", or the "Client URL Library", is a much more verbose method of requesting URL data, allowing many advanced options like timeouts, encoding, and much more.

This method does take some additional setup.

Here is an example to start you off:

$gamertag = "Null Parameter";
$url = "http://www.bungie.net/api/reach/reachapijson.svc/file/share/{apikey}/".rawurlencode($gamertag);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'User App');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$output = curl_exec($ch);
curl_close($ch);

Thanks to OcR Envy for supplying the basis for this code

[edit] File

"File" is the same as file_get_contents, except that it returns an array indexed by line number instead of a string. This would be useful if you ever need data on a line-by-line basis.

$gamertag = "Null Parameter";
$url = "http://www.bungie.net/api/reach/reachapijson.svc/file/share/{apikey}/" . rawurlencode($gamertag);
$output_array = file($url);

In some situations you may need to convert the array into a string, such as when you are going to compress the data for caching. The simplest way to do this is to use implode on the returned array:

$output_string = implode("", $output_array);

[edit] Examples

function GameMetadataExample() {
    // reach api key
    $apiKey = '[API KEY]';
 
    // make a request to bungie for game metadata
    $url = 'http://www.bungie.net/api/reach/reachapijson.svc/game/metadata/' . rawurlencode($apiKey);
    $output = file_get_contents($url);
 
    // try to convert the response into a PHP object
    $obj = json_decode($output);
 
    if(!is_object($obj))
        die("Error parsing JSON response\n");
 
    // display results
    printf("Metadata Service Response: %s", $obj->reason);
    printf("\n\tCommendation Count: %d", count($obj->Data->AllCommendationsById));
    printf("\n\tEnemy Count: %d", count($obj->Data->AllEnemiesById));
    printf("\n\tMedal Count: %d", count($obj->Data->AllMedalsById));
    printf("\n\tMaps Count: %d", count($obj->Data->AllMapsById));
    printf("\n\tWeapons Count: %d", count($obj->Data->AllWeaponsById));
    printf("\n");
}
 
function PlayerDetailsExample() {
    // reach api key
    $apiKey = '[API KEY]';
 
    // player's gamertag
    $playerGamertag = 'ericpp';
 
    // make a request to bungie for player details
    $url = 'http://www.bungie.net/api/reach/reachapijson.svc/player/details/nostats/' . rawurlencode($apiKey) . '/' . rawurlencode($playerGamertag);
    $output = file_get_contents($url);
 
    // try to convert the response into a PHP object
    $obj = json_decode($output);
 
    if(!is_object($obj))
        die("Error parsing JSON response\n");
 
    // show an error if the player doesn't exist
    if(!$obj->Player)
        die("Player not found...");
 
    // display player details
        printf("Player Details: %s (%s)", $obj->Player->gamertag, $obj->Player->service_tag);
 
        // Player model image (standard and Hi Res)
        printf("\nPlayer Model Url: http://bungie.net%s", $obj->PlayerModelUrl);
        printf("\nPlayer Model Url (Hi-Res): http://bungie.net%s", $obj->PlayerModelUrlHiRes);
 
        // Challenges information
        printf("\nDaily Challenges Completed: %d", $obj->Player->daily_challenges_completed);
        printf("\nWeekly Challenges Completed: %d", $obj->Player->weekly_challenges_completed);
 
        // Last game type played
        printf("\nLast Game Type Played: %s", $obj->Player->LastGameVariantClassPlayed);
 
    printf("\n");
}

[edit] Code Libraries

[edit] PHP-Halo-Reach-API-Library

The PHP-Halo-Reach-API-Library is a code library for the Halo: Reach API, written in PHP. It uses the JSON service to create objects from requests for simple inclusion in web applications. The code is hosted at GitHub. See PHP-Halo-Reach-API-Library for more details.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox