Skip to content

richard4339/destiny2-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

destiny2-php

A PHP wrapper for the Destiny 2 API

Latest Stable Version Total Downloads Latest Unstable Version License composer.lock Build Status

About

Currently includes (mostly) just the clan endpoints, with some user endpoints. Basic vendor support has been added as well (Xur's hash is 2190858386 FYI). The goal is to eventually have all endpoints available.

My intention is to make all object calls be JSON Serializable by implementing JsonSerializable()

Installation

Using Composer

composer require richard4339/destiny2-php

Upgrading

To 0.3

  • ClanApproveMember() method now returns an object instead of a boolean. Any calls to this method that check for a boolean must change.
  • ClanKickMember() method now returns a boolean for success instead of the abstracted Guzzle response.

Usage

require 'vendor/autoload.php';

$client = new \Destiny\Client('[YOUR API KEY]', '[OPTIONAL OAUTH TOKEN]', '[OPTIONAL CLIENT ID]', '[OPTIONAL CLIENT SECRET]', '[OPTIONAL YOUR APP NAME]', '[OPTIONAL YOUR APP VERSION]', '[OPTIONAL YOUR APP ID]', '[OPTIONAL YOUR APP URL]', '[OPTIONAL YOUR APP EMAIL]');
  
$clan = $client->getGroup(12345);
  
try {
    $whoami = $client->getBungieUser(9999999999);
} catch (\Destiny\Exceptions\ClientException $x) {
    
}
  
$members = $client->getClanAdminsAndFounder(12345);

The optional App Name, Version, ID, URL, and Email fields were added to populate the User-Agent in the header, which is now recommended by Bungie (and may eventually be required).

Symfony

If you want to use this in a Symfony project, the code below can be used to wire the service in services.yaml for calls that do not require OAuth:

services:
    Destiny\Client:
        arguments:
            $apiKey: '%destiny.api_key%'
            $appName: '[YOUR APP NAME]'
            $appVersion: '[YOUR APP VERSION]'
            $appIDNumber: '[YOUR APP ID]'
            $appURL: '[YOUR APP URL]'
            $appEmail: '[YOUR EMAIL]'

Replace [YOUR APP NAME], [YOUR APP VERSION], [YOUR APP ID], [YOUR APP URL], and [YOUR EMAIL].

It also assumes you have defined your API key defined in your services.yaml as well:

parameters:
    destiny.client_id: '%env(DESTINY_CLIENT_ID)%'
    destiny.client_secret: '%env(DESTINY_CLIENT_SECRET)%'
    destiny.api_key: '%env(DESTINY_API_KEY)%'

For calls that require OAuth, you need to extend Client and wire your token in. The code below is just an example that works using the Security component which can get the token from the User.

<?php

namespace App\Services;


use Destiny\Client;
use Symfony\Component\Security\Core\Security;


/**
 * Class BungieOAuthRequest
 * @package App\Services
 */
class BungieOAuthRequest extends Client
{

    /**
     * BungieOAuthRequest constructor.
     * @param Security $security
     * @param string $apiKey
     * @param null|string $clientID
     * @param null|string $clientSecret
     * @param null|string $appName
     * @param null|string $appVersion
     * @param null|string $appIDNumber
     * @param null|string $appURL
     * @param null|string $appEmail
     * @throws \Destiny\Exceptions\ApiKeyException
     */
    public function __construct(Security $security, string $apiKey = '', ?string $clientID = null, ?string $clientSecret = null, ?string $appName = '', ?string $appVersion = '', ?string $appIDNumber = '', ?string $appURL = '', ?string $appEmail = '')
    {
        $user = $security->getUser();

        $token = $user->getBungieAccessToken();
        parent::__construct($apiKey, $token, $clientID, $clientSecret, $appName, $appVersion, $appIDNumber, $appURL, $appEmail);
    }
}

services.yaml wiring:

services:
    App\Services\BungieOAuthRequest:
        arguments:
            $security: '@security.helper'
            $apiKey: '%destiny.api_key%'
            $clientID: '%destiny.client_id%'
            $clientSecret: '%destiny.client_secret%'
            $appName: '[YOUR APP NAME]'
            $appVersion: '[YOUR APP VERSION]'
            $appIDNumber: '[YOUR APP ID]'
            $appURL: '[YOUR APP URL]'
            $appEmail: '[YOUR EMAIL]'

Requirements

Destiny API Resources

If you need assistance with the Destiny API, there are a bunch of great resources maintained by a few wonderful community members!

And specifically the devs below who build and maintain these community resources