kshabazz / battlenet-d3

此软件包已被废弃,不再维护。未建议替代软件包。

一个用于访问 Battle.net Diablo 3 REST 服务的 API(用 PHP 编写)。

1.3.0 2015-10-19 02:09 UTC

This package is auto-updated.

Last update: 2023-05-14 18:19:49 UTC


README

一个用于访问 Battle.net Diablo 3 REST 服务的 PHP 库。

此 API 提供了访问 Diablo 3 个人资料、英雄和物品的客户端,需要 API 密钥和战斗标签。还包含一些对象模型:Profile(个人资料)、Hero(英雄)、Item(物品)和 Skill(技能,包括主动和被动技能)。

要求

  • PHP 5.4 - 5.6

安装

将其添加到 composer.json 文件中

"require": {
    "kshabazz/battlenet-d3": "^1.2"
}

总结

您可以获取来自 Battle.Net 的原始 JSON 数据,或者使用此库提供的简单模型。

从 Battle.net 获取数据的示例(作为 JSON)

use
    \Kshabazz\Slib\HttpClient,
    \Kshabazz\BattleNet\D3\Connections\Http as D3_Http,
    \Kshabazz\BattleNet\D3\Profile as D3_Profile;

// An API key and Battle.Net Tag are required for all request.
$apiKey       = 'apiKeyFromMashery';
$battleNetTag = 'msuBREAKER#1374';
$heroId       = 3955832;
$itemHash     = 'item/CioI4YeygAgSBwgEFcgYShEdhBF1FR2dbLMUHape7nUwDTiTA0'
              . 'AAUApgkwMYkOPQlAI';

// Get an HTTP client, currently only my custom HTTP client works.
$httpClient = new HttpClient();

// Initialize a battle.net HTTP client.
$d3Client = new D3_Http( $apiKey, $battleNetTag, $httpClient );

// Get the profile for the Battle.net tag (this will be the raw JSON).
$profileJson = $d3Client->getProfile();

// Get the Hero (again, this will be the raw JSON).
$heroJson = $d3Client->getHero( $heroId );

// Get an item (and again, this will be the raw JSON).
// Get the item from Battle.net.
$itemJson = $d3Client->getItem( $itemHash );

var_dump(
    "Profile:" . $profileJson,
    "\nHero:" . $heroJson,
    "\nItem" . $itemJson
);

使用模型(Profile/Hero/Item)的示例

以下示例展示了如何使用此库提供的模型。

<?php
use \Kshabazz\BattleNet\D3\Client as D3_Client;

$apiKey       = 'apiKeyFromMashery';
$battleNetTag = 'msuBREAKER#1374';
$heroId       = 3955832;
$itemHash     = 'item/CioI4YeygAgSBwgEFcgYShEdhBF1FR2dbLMUHape7nUwDTiTA0'
              . 'AAUApgkwMYkOPQlAI';

// Using a factory method:
$d3Client = new D3_Client( $apiKey, $battleNetTag );

// Get a profile from Battle.net and return a Profile model.
$profile = $d3Client->getProfile();

// Get a hero from Battle.net and return a Hero model.
$hero = $d3Client->getHero( $heroId );

// Get an item from Battle.net and return an Item Model.
$item = $d3Client->getItem( $itemHash );

var_dump( $profile, $hero, $item );

使用工厂方法的示例

<?php
use \Kshabazz\BattleNet\D3\Client as D3_Client;

$apiKey       = 'apiKeyFromMashery';
$battleNetTag = 'msuBREAKER#1374';
$heroId       = 3955832;
$itemHash     = 'item/CioI4YeygAgSBwgEFcgYShEdhBF1FR2dbLMUHape7nUwDTiTA0'
              . 'AAUApgkwMYkOPQlAI';

// This is mainly when you need to pull multiple profiles at a time.
$profile = D3_Client::profileFactory( $apiKey, $battleNetTag );

// When you want to grab multiple heroes, even from multiple profiles, at a time.
$hero = D3_Client::heroFactory( $apiKey, $battleNetTag, $heroId );


// Get a list of items from the Hero.
$heroItemHashes = $hero->itemsHashesBySlot();

// Get the item from Battle.net.
$itemJson = $d3Client->getItem( $heroItemHashes['mainHand'] );

// Returns an Array.
var_dump( $profile->heroes() );
var_dump( $profile->json() );
?>

实时示例

您可以在以下位置看到此库的使用示例: http://d3a.kshabazz.net/

实时示例快速链接