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