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