Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 6dc744a

Browse files
committed
Allow making JSON requests via Guzzle
This patch fixes #87, allowing users to make JSON requests via the `GuzzleHttpService`. In fact, it goes a step further, and allows passing a fully formed request instance to the `GuzzleHttpService` constructor, allowing full customization of the request to send.
1 parent df9919e commit 6dc744a

File tree

3 files changed

+328
-76
lines changed

3 files changed

+328
-76
lines changed

docs/book/diagnostics.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,37 @@ $checkPageContent = new HttpService(
192192
## GuzzleHttpService
193193

194194
Attempt connection to a given HTTP host or IP address and try to load a web page
195-
using [Guzzle](http://guzzle3.readthedocs.org/en/latest/). The check also
196-
supports checking response codes and page contents.
195+
using [Guzzle](http://docs.guzzlephp.org). The check also supports checking
196+
response codes and page contents.
197+
198+
The constructor signature of the `GuzzleHttpService` is as follows:
199+
200+
```php
201+
/**
202+
* @param string|Psr\Http\Message\RequestInterface|GuzzleHttp\Message\RequestInterface $requestOrUrl
203+
* The absolute url to check, or a fully-formed request instance.
204+
* @param array $headers An array of headers used to create the request
205+
* @param array $options An array of guzzle options to use when sending the request
206+
* @param int $statusCode The response status code to check
207+
* @param null $content The response content to check
208+
* @param null|GuzzleHttp\ClientInterface $guzzle Instance of guzzle to use
209+
* @param string $method The method of the request
210+
* @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
211+
* @throws InvalidArgumentException
212+
*/
213+
public function __construct(
214+
$requestOrUrl,
215+
array $headers = [],
216+
array $options = [],
217+
$statusCode = 200,
218+
$content = null,
219+
$guzzle = null,
220+
$method = 'GET',
221+
$body = null
222+
)
223+
```
224+
225+
Examples:
197226

198227
```php
199228
<?php
@@ -229,6 +258,33 @@ $checkPageContent = new GuzzleHttpService(
229258
);
230259
```
231260

261+
You can send JSON data by either providing a `Content-Type` header that includes
262+
a JSON content type, or creating a request instance with JSON content:
263+
264+
```php
265+
// Send page content
266+
$checkPageContent = new GuzzleHttpService(
267+
'api.example.com/ping',
268+
['Content-Type' => 'application/json'],
269+
[],
270+
200,
271+
null,
272+
null,
273+
'POST',
274+
['ping' => microtime()]
275+
);
276+
277+
// Assuming Guzzle 6:
278+
use GuzzleHttp\Psr7\Request;
279+
$request = new Request(
280+
'POST',
281+
'http://api.example.com/ping',
282+
['Content-Type' => 'application/json'],
283+
json_encode(['ping' => microtime()])
284+
);
285+
$checkPageContent = new GuzzleHttpService($request);
286+
```
287+
232288
## Memcache
233289

234290
Attempt to connect to given Memcache server.

0 commit comments

Comments
 (0)