1
|
|
|
<?php namespace Syntax\SteamApi; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
5
|
|
|
use Guzzle\Http\Exception\ServerErrorResponseException; |
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
use stdClass; |
8
|
|
|
use Syntax\SteamApi\Exceptions\ApiCallFailedException; |
9
|
|
|
use Syntax\SteamApi\Exceptions\ClassNotFoundException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method news() |
13
|
|
|
* @method player($steamId) |
14
|
|
|
* @method user($steamId) |
15
|
|
|
* @method userStats($steamId) |
16
|
|
|
* @method app() |
17
|
|
|
* @method group() |
18
|
|
|
*/ |
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use SteamId; |
23
|
|
|
|
24
|
|
|
public $validFormats = ['json', 'xml', 'vdf']; |
25
|
|
|
|
26
|
|
|
protected $url = 'http://api.steampowered.com/'; |
27
|
|
|
|
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
protected $interface; |
31
|
|
|
|
32
|
|
|
protected $method; |
33
|
|
|
|
34
|
|
|
protected $version = 'v0002'; |
35
|
|
|
|
36
|
|
|
protected $apiKey; |
37
|
|
|
|
38
|
|
|
protected $apiFormat = 'json'; |
39
|
|
|
|
40
|
|
|
protected $steamId; |
41
|
|
|
|
42
|
|
|
protected $isService = false; |
43
|
|
|
|
44
|
41 |
|
public function __construct() |
45
|
|
|
{ |
46
|
41 |
|
$apiKey = $this->getApiKey(); |
47
|
|
|
|
48
|
41 |
|
$this->client = new GuzzleClient(['base_uri' => $this->url]); |
49
|
|
|
|
50
|
41 |
|
$this->apiKey = $apiKey; |
51
|
|
|
|
52
|
|
|
// Set up the Ids |
53
|
41 |
|
$this->setUpFormatted(); |
54
|
41 |
|
} |
55
|
|
|
|
56
|
|
|
public function get() |
57
|
|
|
{ |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function getSteamId() |
62
|
|
|
{ |
63
|
1 |
|
return $this->steamId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $arguments |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
* |
71
|
|
|
* @throws ApiArgumentRequired |
72
|
|
|
* @throws ApiCallFailedException |
73
|
|
|
*/ |
74
|
10 |
|
protected function setUpService($arguments = null) |
75
|
|
|
{ |
76
|
|
|
// Services have a different url syntax |
77
|
10 |
|
if ($arguments == null) { |
|
|
|
|
78
|
|
|
throw new ApiArgumentRequired; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$parameters = [ |
82
|
10 |
|
'key' => $this->apiKey, |
83
|
10 |
|
'format' => $this->apiFormat, |
84
|
10 |
|
'input_json' => $arguments, |
85
|
10 |
|
]; |
86
|
|
|
|
87
|
10 |
|
$steamUrl = $this->buildUrl(true); |
88
|
|
|
|
89
|
|
|
// Build the query string |
90
|
10 |
|
$parameters = http_build_query($parameters); |
91
|
|
|
|
92
|
|
|
// Send the request and get the results |
93
|
10 |
|
$request = $this->client->get($steamUrl . '?' . $parameters); |
94
|
|
|
$response = $this->sendRequest($request); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// Pass the results back |
97
|
|
|
return $response->body; |
98
|
|
|
} |
99
|
|
|
|
100
|
17 |
|
protected function setUpClient(array $arguments = []) |
101
|
|
|
{ |
102
|
17 |
|
$versionFlag = !is_null($this->version); |
103
|
17 |
|
$steamUrl = $this->buildUrl($versionFlag); |
104
|
|
|
|
105
|
|
|
$parameters = [ |
106
|
17 |
|
'key' => $this->apiKey, |
107
|
17 |
|
'format' => $this->apiFormat |
108
|
17 |
|
]; |
109
|
|
|
|
110
|
17 |
|
if (!empty($arguments)) { |
111
|
16 |
|
$parameters = array_merge($arguments, $parameters); |
112
|
16 |
|
} |
113
|
|
|
|
114
|
|
|
// Build the query string |
115
|
17 |
|
$parameters = http_build_query($parameters); |
116
|
|
|
|
117
|
|
|
// Send the request and get the results |
118
|
17 |
|
$request = $this->client->get($steamUrl . '?' . $parameters); |
119
|
6 |
|
$response = $this->sendRequest($request); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// Pass the results back |
122
|
|
|
return $response->body; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
protected function setUpXml(array $arguments = []) |
126
|
|
|
{ |
127
|
4 |
|
$steamUrl = $this->buildUrl(); |
128
|
|
|
|
129
|
|
|
// Build the query string |
130
|
4 |
|
$parameters = http_build_query($arguments); |
131
|
|
|
|
132
|
|
|
// Pass the results back |
133
|
4 |
|
return simplexml_load_file($steamUrl . '?' . $parameters); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \Guzzle\Http\Message\RequestInterface $request |
138
|
|
|
* |
139
|
|
|
* @throws ApiCallFailedException |
140
|
|
|
* @return stdClass |
141
|
|
|
*/ |
142
|
6 |
|
protected function sendRequest($request) |
143
|
|
|
{ |
144
|
|
|
// Try to get the result. Handle the possible exceptions that can arise |
145
|
|
|
try { |
146
|
6 |
|
$response = $this->client->send($request); |
147
|
|
|
|
148
|
|
|
$result = new stdClass(); |
149
|
|
|
$result->code = $response->getStatusCode(); |
150
|
|
|
$result->body = json_decode($response->getBody(true)); |
|
|
|
|
151
|
|
|
|
152
|
6 |
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
153
|
|
|
throw new ApiCallFailedException($e->getMessage(), $e->getResponse()->getStatusCode(), $e); |
154
|
|
|
|
155
|
6 |
|
} catch (ServerErrorResponseException $e) { |
|
|
|
|
156
|
|
|
throw new ApiCallFailedException('Api call failed to complete due to a server error.', |
157
|
|
|
$e->getResponse()->getStatusCode(), $e); |
158
|
|
|
|
159
|
6 |
|
} catch (Exception $e) { |
160
|
6 |
|
throw new ApiCallFailedException($e->getMessage(), $e->getCode(), $e); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// If all worked out, return the result |
165
|
|
|
return $result; |
166
|
|
|
} |
167
|
|
|
|
168
|
30 |
|
private function buildUrl($version = false) |
169
|
|
|
{ |
170
|
|
|
// Set up the basic url |
171
|
30 |
|
$url = $this->url . $this->interface . '/' . $this->method . '/'; |
172
|
|
|
|
173
|
|
|
// If we have a version, add it |
174
|
30 |
|
if ($version) { |
175
|
25 |
|
return $url . $this->version . '/'; |
176
|
|
|
} |
177
|
|
|
|
178
|
5 |
|
return $url; |
179
|
|
|
} |
180
|
|
|
|
181
|
33 |
|
public function __call($name, $arguments) |
182
|
|
|
{ |
183
|
|
|
// Handle a steamId being passed |
184
|
33 |
|
if (!empty($arguments) && count($arguments) == 1) { |
185
|
27 |
|
$this->steamId = $arguments[0]; |
186
|
|
|
|
187
|
27 |
|
$this->convertSteamIdTo64(); |
188
|
27 |
|
} |
189
|
|
|
|
190
|
|
|
// Inside the root steam directory |
191
|
33 |
|
$class = ucfirst($name); |
192
|
33 |
|
$steamClass = '\Syntax\SteamApi\Steam\\' . $class; |
193
|
|
|
|
194
|
33 |
|
if (class_exists($steamClass)) { |
195
|
28 |
|
return new $steamClass($this->steamId); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Inside a nested directory |
199
|
6 |
|
$class = implode('\\', preg_split('/(?=[A-Z])/', $class, -1, PREG_SPLIT_NO_EMPTY)); |
200
|
6 |
|
$steamClass = '\Syntax\SteamApi\Steam\\' . $class; |
201
|
|
|
|
202
|
6 |
|
if (class_exists($steamClass)) { |
203
|
6 |
|
return new $steamClass($this->steamId); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Nothing found |
207
|
|
|
throw new ClassNotFoundException($name); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param Collection $objects |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
protected function sortObjects($objects) |
216
|
|
|
{ |
217
|
|
|
return $objects->sortBy(function ($object) { |
218
|
|
|
return $object->name; |
219
|
|
|
}); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $method |
224
|
|
|
* @param string $version |
225
|
|
|
*/ |
226
|
10 |
|
protected function setApiDetails($method, $version) |
227
|
|
|
{ |
228
|
10 |
|
$this->method = $method; |
229
|
10 |
|
$this->version = $version; |
230
|
10 |
|
} |
231
|
|
|
|
232
|
10 |
|
protected function getServiceResponse($arguments) |
233
|
|
|
{ |
234
|
10 |
|
$arguments = json_encode($arguments); |
235
|
|
|
|
236
|
|
|
// Get the client |
237
|
10 |
|
$client = $this->setUpService($arguments)->response; |
238
|
|
|
|
239
|
|
|
return $client; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
* @throws Exceptions\InvalidApiKeyException |
245
|
|
|
*/ |
246
|
41 |
|
protected function getApiKey() |
247
|
|
|
{ |
248
|
41 |
|
$apiKey = \Config::get('steam-api.steamApiKey'); |
249
|
|
|
|
250
|
41 |
|
if ($apiKey == 'YOUR-API-KEY') { |
251
|
|
|
throw new Exceptions\InvalidApiKeyException(); |
252
|
|
|
} |
253
|
41 |
|
if (is_null($apiKey) || $apiKey == '' || $apiKey == []) { |
254
|
41 |
|
$apiKey = getenv('apiKey'); |
255
|
41 |
|
} |
256
|
|
|
|
257
|
41 |
|
return $apiKey; |
258
|
|
|
} |
259
|
|
|
|
260
|
27 |
|
private function convertSteamIdTo64() |
261
|
|
|
{ |
262
|
27 |
|
if (is_array($this->steamId)) { |
263
|
1 |
|
array_walk($this->steamId, function (&$id) { |
264
|
1 |
|
if (strpos($id, ':') !== false) { |
265
|
|
|
// Convert the id to all types and grab the 64 bit version |
266
|
1 |
|
$id = $this->convertToAll($id)->id64; |
267
|
1 |
|
} |
268
|
1 |
|
}); |
269
|
27 |
|
} elseif (strpos(':', $this->steamId) !== false) { |
270
|
|
|
// Convert the id to all types and grab the 64 bit version |
271
|
|
|
$this->steamId = $this->convertToAll($this->steamId)->id64; |
272
|
|
|
} |
273
|
27 |
|
} |
274
|
|
|
} |
275
|
|
|
|