-
Notifications
You must be signed in to change notification settings - Fork 0
/
diablo3-api.inc.php
416 lines (373 loc) · 11.4 KB
/
diablo3-api.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
*
* @author Tom Green <[email protected]>
* @link https://bitbucket.org/BeingTomGreen/diablo-3-api
* @license MIT - http://beingtomgreen.mit-license.org/
*
* A basic PHP wrapper for the integrating the Diablo 3 API into your application.
*
*/
class D3 {
// Hold the URL parts
private $protocol = 'http://';
private $server = 'eu';
private $host = '.battle.net';
private $apiSlug = 'api/d3/';
private $mediaSlug = 'd3/static/images/';
private $locale = 'en_GB';
// Hold the API keys
public $publicKey;
public $privateKey;
// Do we want to make authenticated requests?
public $authenticate = false;
// Hold the built URLs
private $apiURL;
private $mediaURL;
// These hold the possible protocols, servers & locals
private $possibleProtocols = ['http://', 'https://'];
private $possibleServers = ['us', 'eu', 'tw', 'kr', 'cn'];
private $possibleLocale = ['en_US', 'en_GB', 'es_MX', 'es_ES', 'it_IT', 'pt_PT', 'pt_BR', 'fr_FR', 'ru_RU', 'pl_PL', 'de_DE', 'ko_KR', 'zh_TW', 'zh_CN'];
// These hold the possibilities for various inputs to be checked against
private $possibleFollowers = ['enchantress', 'templar', 'scoundrel'];
private $possibleArtisans = ['blacksmith', 'jeweler'];
private $possibleClasses = ['barbarian', 'witch-doctor', 'demon-hunter', 'monk', 'wizard'];
private $possibleGenders = ['male', 'female'];
// These hold the Regular Expressions for various inputs to be checked against
private $battleTagPattern = '/^[\p{L}\p{Mn}][\p{L}\p{Mn}0-9]{2,11}-[0-9]{4,5}+$/u'; // - https://github.com/XjSv/Diablo-3-API-PHP/issues/4#issuecomment-15982672
private $heroIDPattern = '/^[\d]+$/';
private $itemIDPattern = '/^[A-Za-z0-9]+$/';
// This allows developers to add additional CURL options
// Can be any of the CURL constants defined here - http://php.net/manual/en/function.curl-setopt.php
public $extraCURLOptions;
/**
* __construct
*
* @param array $args - Optional array of settings (protocol, server and locale)
*
*/
function __construct ($args = null)
{
// Check if we have been passed a valid protocol and if so use it
if (isset($args['protocol']) and in_array($args['protocol'], $this->possibleProtocols))
{
$this->protocol = $args['protocol'];
}
// Check if we have been passed a valid server and if so use it
if (isset($args['server']) and in_array($args['server'], $this->possibleServers))
{
$this->server = $args['server'];
}
// Check if we have been passed a valid locale and if so use it
if (isset($args['locale']) and in_array($args['locale'], $this->possibleLocale))
{
$this->locale = $args['locale'];
}
// Lets build the main part of the URLs to save us repeating ourselves
$this->apiURL = $this->protocol . $this->server . $this->host . '/' . $this->apiSlug;
$this->mediaURL = $this->protocol . $this->server . $this->host . '/' . $this->mediaSlug;
}
/**
* getCareer
*
* Returns the Career data
*
* @param string $battleTag - the users Battle Tag
*
* @return json/false - data if we have it, otherwise false
*
*/
public function getCareer($battleTag)
{
// Replace '#' with '-' as some users may enter it with '#'
$battleTag = str_replace('#', '-', $battleTag);
// Check that the Battle Tag is valid
if ($this->validBattleTag($battleTag) == true)
{
// Build the URL
$url = sprintf(
$this->apiURL .'profile/%s/?locale='. $this->locale,
$battleTag
);
// Grab the data
return $this->makeCURLCall($url);
}
// Battle Tag not valid - make a note of it and return false
else
{
error_log('Invalid BattleTag ('. $battleTag .')');
return false;
}
}
/**
* getHero
*
* Returns the Hero data
*
* @param string $battleTag - the users Battle Tag
* @param string $herID - the Hero ID
*
* @return json/false - data if we have it, otherwise false
*
*/
public function getHero($battleTag, $heroID)
{
// Replace '#' with '-' as some users may enter it with '#'
$battleTag = str_replace('#', '-', $battleTag);
// Check that the Battle Tag and Hero ID are valid
if ($this->validBattleTag($battleTag) == true and $this->validHeroID($heroID) == true)
{
// Build the URL
$url = sprintf(
$this->apiURL .'profile/%s/hero/%d?locale'. $this->locale,
$battleTag,
$heroID
);
// Grab the data
return $this->makeCURLCall($url);
}
// Battle Tag or Hero ID not valid - make a note of it and return false
else
{
error_log('Invalid Battle Tag or Hero ID (Battle Tag: '. $battleTag .'Hero ID: '. $heroID .')');
return false;
}
}
/**
* getItem
*
* Returns the Item data
*
* @param string $itemID - the Item ID
*
* @return json/false - data if we have it, otherwise false
*
*/
public function getItem($itemID)
{
// Check that the Item ID is valid
if ($this->validItemID($itemID) == true)
{
// Build the URL
$url = sprintf(
$this->apiURL .'data/item/%s?locale='. $this->locale,
$itemID
);
// Grab the data
return $this->makeCURLCall($url);
}
// Item ID not valid - make a note of it and return false
else
{
error_log('Invalid Item ID ('. $itemID .')');
return false;
}
}
/**
* getFollower
*
* Returns the Follower data
*
* @param string $followerType - the Follower type
*
* @return json/false - data if we have it, otherwise false
*
*/
public function getFollower($followerType)
{
// Make the Follower lowercase
$followerType = strtolower($followerType);
// Check that the Follower is valid
if (in_array($followerType, $this->possibleFollowers))
{
// Build the URL
$url = sprintf(
$this->apiURL .'data/follower/%s?locale='. $this->locale,
strtolower($followerType)
);
// Grab the data
return $this->makeCURLCall($url);
}
// Follower not valid - make a note of it and return false
else
{
error_log('Invalid Follower ('. $followerType .')');
return false;
}
}
/**
* getArtisan
*
* Returns the Artisan data
*
* @param string $artisanType - the Artisan type
*
* @return json/false - data if we have it, otherwise false
*
*/
public function getArtisan($artisanType)
{
// Make the Artisan lowercase
$artisanType = strtolower($artisanType);
// Check that the Artisan is valid
if (in_array($artisanType, $this->possibleArtisans))
{
// Build the URL
$url = sprintf(
$this->apiURL .'data/artisan/%s?locale='. $this->locale,
strtolower($artisanType)
);
// Grab the data
return $this->makeCURLCall($url);
}
// Artisan not valid - make a note of it and return false
else
{
error_log('Invalid Artisan ('. $artisanType .')');
return false;
}
}
/**
* getPaperDoll
*
* Returns a link to the Paper Doll image
*
* @param string $class - the Class
* @param string $gender - the Gender
*
* @return string/false - data if we have it, otherwise false
*
*/
public function getPaperDoll($classType, $genderType)
{
// Check that the Class and Gender are valid
if (in_array(strtolower($classType), $this->possibleClasses) and in_array(strtolower($genderType), $this->possibleGenders))
{
// Build and return the URL
return sprintf(
$this->mediaURL .'profile/hero/paperdoll/%s-%s.jpg',
strtolower($classType),
strtolower($genderType)
);
}
// Class or Gender not valid - make a note of it and return false
else
{
error_log('Invalid Class or Gender (Class: '. $classType .' Gender: '. $genderType .')');
return false;
}
}
/**
* makeCURLCall
*
* Makes the specified CURL request - this is the meat of the class!
*
* @param string $url - The URL for the CURL request
*
* @return json/bool - data if we have it, otherwise false
*
*/
private function makeCURLCall($url)
{
// Initialise CURL
$handle = curl_init();
// Do we have any extra CURL options?
if (is_array($this->extraCURLOptions) and !empty($this->extraCURLOptions))
{
// Set any extra CURL options
curl_setopt_array($handle, $this->extraCURLOptions);
}
// Set the CURL options we need
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
// Are we trying to make an authenticated request, and do we have the required keys
if ($this->authenticate === true and $this->publicKey != '' and $this->privateKey !='') {
// Set the API endpoint we are requesting
$urlPath = str_replace($this->protocol . $this->server . $this->host, '', $url);
// Current date time (format should be: 'Fri, 10 Jun 2011 21:37:34 <TIMEZONE>')
$requestTime = date('D, d M Y G:i:s e', time());
// Set up the authentication signature
$authSignature = base64_encode(hash_hmac('sha1', 'GET'. PHP_EOL . $requestTime . PHP_EOL . $urlPath . PHP_EOL, $this->privateKey, true));
// Set the HTTP header
curl_setopt($handle, CURLOPT_HTTPHEADER, [
'Host: '. $this->server . $this->host,
'Date: '. $requestTime,
PHP_EOL .'Authorization: BNET '. $this->publicKey .":". $authSignature. "\n"
]);
}
// Grab the data
$data = curl_exec($handle);
// Grab the CURL error code and message
$errorCode = curl_errno($handle);
$errorMessage = curl_error($handle);
// Close the CURL connection
curl_close($handle);
// Check our error code is 0 (0 means OK!)
if ($errorCode == 0)
{
// Decode the json response
$data = json_decode($data, true);
// Check we don't have an error code
if (isset($data['code']) and isset($data['reason']))
{
// API error - make a note of it and return false
error_log('API error: '. $data['code'] .' - '. $data['reason'] .' ('. $url .')!');
return false;
}
// No errors - cache and return the data
else
{
// Return the data
return $data;
}
}
// CURL error - make a note of it and return false
else
{
error_log('CURL error "'. $errorCode .'" ('. $errorMessage .').');
return false;
}
}
/**
* validBattleTag
*
* Checks that a supplied Battle Tag is valid - according to https://us.battle.net/support/en/article/BattleTagNamingPolicy
*
* @param string $battleTag - The Battle Tag
*
* @return bool - is the Battle Tag valid or not?
*
*/
public function validBattleTag ($battleTag)
{
return preg_match($this->battleTagPattern, $battleTag) ? true : false;
}
/**
* validHeroID
*
* Checks that a supplied Hero ID is valid
*
* @param string $heroID - The Hero ID
*
* @return bool - is the HeroID valid or not?
*
*/
public function validHeroID ($heroID)
{
return preg_match($this->heroIDPattern, $heroID) ? true : false;
}
/**
* validItemID
*
* Checks that a supplied Item ID is valid
*
* @param string $itemID - The Item ID
*
* @return bool - is the Item ID valid or not?
*
*/
public function validItemID ($itemID)
{
return preg_match($this->itemIDPattern, $itemID) ? true : false;
}
}