|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sl; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as Guzzle; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Sl\Contracts\HttpClientInterface; |
|
8
|
|
|
|
|
9
|
|
|
class HttpClient extends Guzzle implements HttpClientInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Base uri. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $baseUri; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a new instance of HttpClient. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $baseUri |
|
22
|
|
|
*/ |
|
23
|
5 |
|
public function __construct($baseUri) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$this->baseUri = $baseUri; |
|
26
|
|
|
|
|
27
|
5 |
|
parent::__construct([ |
|
28
|
5 |
|
'base_uri' => $baseUri, |
|
29
|
|
|
'headers' => [ |
|
30
|
5 |
|
'Accept' => 'application/json', |
|
31
|
5 |
|
'Accept-Encoding' => 'gzip, deflate, sdch', |
|
32
|
5 |
|
'Accept-Language' => 'en-US', |
|
33
|
5 |
|
'Host' => 'sl.se', |
|
34
|
5 |
|
'Referer' => 'http://sl.se/', |
|
35
|
5 |
|
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', |
|
36
|
5 |
|
], |
|
37
|
5 |
|
]); |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get base uri. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function baseUri() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->baseUri; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Send HTTP GET request. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $uri |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function get($uri) |
|
58
|
|
|
{ |
|
59
|
4 |
|
return $this->request('GET', $uri); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse JSON response. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function parseJsonResponse(ResponseInterface $response) |
|
70
|
|
|
{ |
|
71
|
3 |
|
return json_decode($response->getBody(), 1); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Send HTTP GET request and JSON response. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $uri |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function getAndParseJson($uri) |
|
82
|
|
|
{ |
|
83
|
3 |
|
$response = $this->get($uri); |
|
84
|
|
|
|
|
85
|
3 |
|
return $this->parseJsonResponse($response); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|