|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Http\ServiceAdapter\GuzzleHttp; |
|
15
|
|
|
|
|
16
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
17
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
19
|
|
|
use WebHemi\Http\ClientInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class HttpClient |
|
23
|
|
|
* |
|
24
|
|
|
* @codeCoverageIgnore - don't test third party library until this only a forwards the calls. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Client implements ClientInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var GuzzleClient |
|
30
|
|
|
*/ |
|
31
|
|
|
private $guzzleClient; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* HttpClient constructor. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->guzzleClient = new GuzzleClient(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get data. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* @param array $data |
|
46
|
|
|
* @return PsrResponseInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function get(string $url, array $data) : PsrResponseInterface |
|
49
|
|
|
{ |
|
50
|
|
|
$queryData = empty($data) |
|
51
|
|
|
? [] |
|
52
|
|
|
: [ |
|
53
|
|
|
'query' => $data |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
return $this->request('GET', $url, $queryData); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Posts data. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $url |
|
63
|
|
|
* @param array $data |
|
64
|
|
|
* @return PsrResponseInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
public function post(string $url, array $data) : PsrResponseInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$formData = []; |
|
69
|
|
|
|
|
70
|
|
|
if (!empty($data)) { |
|
71
|
|
|
$formData['multipart'] = []; |
|
72
|
|
|
|
|
73
|
|
|
foreach ($data as $key => $value) { |
|
74
|
|
|
$formData['multipart'][] = [ |
|
75
|
|
|
'name' => (string) $key, |
|
76
|
|
|
'contents' => (string) $value |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->request('POST', $url, $formData); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Request an URL with data. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $method |
|
88
|
|
|
* @param string $url |
|
89
|
|
|
* @param array $options |
|
90
|
|
|
* @return PsrResponseInterface |
|
91
|
|
|
*/ |
|
92
|
|
|
private function request(string $method, string $url, array $options) : PsrResponseInterface |
|
93
|
|
|
{ |
|
94
|
|
|
try { |
|
95
|
|
|
$response = $this->guzzleClient->request($method, $url, $options); |
|
96
|
|
|
} catch (RequestException $exception) { |
|
97
|
|
|
$response = $exception->getResponse(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $response; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|