|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ClashApi\DataAccessLayer |
|
4
|
|
|
{ |
|
5
|
|
|
use ClashApi\Exceptions\ApiException; |
|
6
|
|
|
|
|
7
|
|
|
class WebClient |
|
8
|
|
|
{ |
|
9
|
|
|
private $apiKey; |
|
10
|
|
|
private $baseURL; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct($apiKey) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->baseURL = 'https://api.clashofclans.com/v1'; |
|
15
|
|
|
$this->apiKey = $apiKey; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Send a Request to SuperCell's Servers and Receive it |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $url |
|
22
|
|
|
* @return stdClass response from API |
|
23
|
|
|
*/ |
|
24
|
|
|
public function sendRequest($url) |
|
25
|
|
|
{ |
|
26
|
|
|
$url = $this->baseURL . $this->encodeHashTag($url); |
|
27
|
|
|
|
|
28
|
|
|
$ch = curl_init(); |
|
29
|
|
|
|
|
30
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
31
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
32
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
33
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
34
|
|
|
'authorization: Bearer '.$this->apiKey |
|
35
|
|
|
)); |
|
36
|
|
|
|
|
37
|
|
|
$response = curl_exec($ch); |
|
38
|
|
|
|
|
39
|
|
|
$info = curl_getinfo($ch); |
|
40
|
|
|
|
|
41
|
|
|
if ($info['http_code'] != 200) { |
|
42
|
|
|
throw new ApiException('The request was not successfull.', $info['http_code'], $response); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
curl_close($ch); |
|
46
|
|
|
|
|
47
|
|
|
returnjson_decode($response); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $url what needs to be encoded |
|
52
|
|
|
* @var string encoded url |
|
53
|
|
|
*/ |
|
54
|
|
|
private function encodeHashTag($url) |
|
55
|
|
|
{ |
|
56
|
|
|
return str_replace("#", urlencode('#'), $url); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|