1 | <?php |
||
9 | class HttpClient |
||
10 | { |
||
11 | /** |
||
12 | * The Guzzle client. |
||
13 | * |
||
14 | * @var \GuzzleHttp\Client |
||
15 | */ |
||
16 | protected $client; |
||
17 | |||
18 | /** |
||
19 | * The Guzzle response. |
||
20 | * |
||
21 | * @var \GuzzleHttp\Psr7\Response |
||
22 | */ |
||
23 | protected $response; |
||
24 | |||
25 | /** |
||
26 | * Create a http client instance. |
||
27 | * |
||
28 | * @param array $config |
||
29 | */ |
||
30 | public function __construct($config = []) |
||
39 | |||
40 | /** |
||
41 | * Get the Guzzle client instance. |
||
42 | * |
||
43 | * @return \GuzzleHttp\Client |
||
44 | */ |
||
45 | public function getClient() |
||
49 | |||
50 | /** |
||
51 | * Get the Guzzle response instance. |
||
52 | * |
||
53 | * @return \GuzzleHttp\Psr7\Response|null |
||
54 | */ |
||
55 | public function getResponse() |
||
59 | |||
60 | /** |
||
61 | * Get response body. |
||
62 | * |
||
63 | * @return \GuzzleHttp\Psr7\Stream|null |
||
64 | */ |
||
65 | public function getBody() |
||
71 | |||
72 | /** |
||
73 | * Get response content. |
||
74 | * |
||
75 | * @return string|null |
||
76 | */ |
||
77 | public function getContent() |
||
83 | |||
84 | /** |
||
85 | * Get JSON decoded response content. |
||
86 | * |
||
87 | * @param bool $assoc |
||
88 | * @return mixed |
||
89 | */ |
||
90 | public function getJson($assoc = true) |
||
96 | |||
97 | /** |
||
98 | * Make request to an URL. |
||
99 | * |
||
100 | * @param string $url |
||
101 | * @param string $method |
||
102 | * @param array $options |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function request($url, $method = 'GET', $options = []) |
||
114 | |||
115 | /** |
||
116 | * Make request to an URL, expecting JSON content. |
||
117 | * |
||
118 | * @param string $url |
||
119 | * @param string $method |
||
120 | * @param array $options |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function requestJson($url, $method = 'GET', $options = []) |
||
129 | |||
130 | /** |
||
131 | * Request the URL and return the response content. |
||
132 | * |
||
133 | * @param string $url |
||
134 | * @param string $method |
||
135 | * @param array $options |
||
136 | * @return string|null |
||
137 | */ |
||
138 | public function fetchContent($url, $method = 'GET', $options = []) |
||
142 | |||
143 | /** |
||
144 | * Request the URL and return the JSON decoded response content. |
||
145 | * |
||
146 | * @param string $url |
||
147 | * @param string $method |
||
148 | * @param array $options |
||
149 | * @param bool $assoc |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
156 | } |
||
157 |