Skip to content

Commit adf476a

Browse files
author
Martin Brecht-Precht
committed
Updated readme.
1 parent 6361a83 commit adf476a

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

Diff for: README.md

+80-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ use JsonHttpClient\Request\JsonRequest;
198198
199199
// Configuring and performing a Request
200200
$request = new JsonRequest();
201-
$response = $request
201+
$request
202202
->setUserAgent('PHP JSON HTTP Client Test 1.0')
203203
->setEndpoint('https://yourapihere-com-98yq3775xff0.runscope.net/')
204204
->setPort(443)
@@ -273,21 +273,82 @@ $response = $request->addAuthentication($clientCertificateAuthentication);
273273

274274
---
275275

276-
## Reading from the resulting Response instance
276+
## Reading from the resulting Response object
277277

278-
TODO
278+
### Getting the response object
279+
280+
If using the `JsonHttpClient` the response object is returned by the termination methods listed above. If directly using the JsonRequest instance, you can get the JsonResponse object via a getter.
281+
282+
```{php}
283+
// Getting the response BasicHttpClient\Response\JsonResponse object
284+
$response = $request->getResponse();
285+
286+
// Reading the HTTP status code as integer; will return `200`
287+
echo print_r($response->getStatusCode(), true) . PHP_EOL;
288+
289+
// Reading the HTTP status text as string; will return `HTTP/1.1 200 OK`
290+
echo print_r($response->getStatusText(), true) . PHP_EOL;
291+
292+
// Reading the HTTP response headers as array of BasicHttpClient\Response\Header\Header objects
293+
echo print_r($response->getHeaders(), true) . PHP_EOL;
294+
295+
// Reading the HTTP response body as associative array
296+
echo print_r($response->getBody(), true) . PHP_EOL;
297+
```
279298

280299
---
281300

282301
## Getting effective Request information
283302

284-
TODO
303+
After successful performing the request, the effective request information is tracked back to the JsonRequest object. They can get accessed as follows.
304+
305+
```{php}
306+
// Getting the effective endpoint URL including the query parameters
307+
echo print_r($request->getEffectiveEndpoint(), true) . PHP_EOL;
308+
309+
// Getting the effective HTTP status, f.e. `POST /?paramName1=paramValue1&paramName2=paramValue2&paramName3=1&paramName4=42 HTTP/1.1`
310+
echo print_r($request->getEffectiveStatus(), true) . PHP_EOL;
311+
312+
// Getting the effective raw request headers as string
313+
echo print_r($request->getEffectiveRawHeader(), true) . PHP_EOL;
314+
315+
// Getting the effective request headers as array of `BasicHttpClient\Request\Message\Header\Header` objects
316+
echo print_r($request->getEffectiveHeaders(), true) . PHP_EOL.PHP_EOL;
317+
```
285318

286319
---
287320

288321
## Getting some transactional statistics
289322

290-
TODO
323+
```{php}
324+
// Getting the statistics BasicHttpClient\Response\Statistics\Statistics object
325+
$statistics = $request->getResponse()->getStatistics();
326+
327+
// Reading the redirection URL if the server responds with an redirect HTTP header and
328+
// followRedirects is set to false
329+
echo print_r($statistics->getRedirectEndpoint(), true).PHP_EOL;
330+
331+
// Reading the numbers of redirection as integer
332+
echo print_r($statistics->getRedirectCount(), true).PHP_EOL;
333+
334+
// Getting the time in seconds the redirect utilized as float
335+
echo print_r($statistics->getRedirectTime(), true).PHP_EOL;
336+
337+
// Getting the time in seconds that was utilized until the connection was established
338+
echo print_r($statistics->getConnectionEstablishTime(), true).PHP_EOL;
339+
340+
// Getting the time in seconds that was utilized until the DNS hostname lookup was done
341+
echo print_r($statistics->getHostLookupTime(), true).PHP_EOL;
342+
343+
// Getting the time in seconds that was utilized before the first data was sent
344+
echo print_r($statistics->getPreTransferTime(), true).PHP_EOL;
345+
346+
// Getting the time in seconds that was utilized before the first data was received
347+
echo print_r($statistics->getStartTransferTime(), true).PHP_EOL;
348+
349+
// Getting the time in seconds that was utilized to perfom the request an read the response
350+
echo print_r($statistics->getTotalTime(), true).PHP_EOL;
351+
```
291352

292353
---
293354

@@ -296,6 +357,20 @@ TODO
296357
PHP JSON HTTP Client provides different exceptions – also provided by the PHP Common Exceptions project – for proper handling.
297358
You can find more information about [PHP Common Exceptions at Github](https://github.com/markenwerk/php-common-exceptions).
298359

360+
### Exceptions to be expected
361+
362+
In general you should expect that any setter method could thrown an `\InvalidArgumentException`. The following exceptions could get thrown while using PHP Basic HTTP Client.
363+
364+
- `CommonException\IoException\FileNotFoundException` on configuring a `ClientCertificateAuthentication`instance
365+
- `CommonException\IoException\FileReadableException` on configuring a `ClientCertificateAuthentication`instance
366+
- `BasicHttpClient\Exception\HttpRequestAuthenticationException` on performing a request
367+
- `BasicHttpClient\Exception\HttpRequestException` on performing a request
368+
- `CommonException\NetworkException\ConnectionTimeoutException` on performing a request
369+
- `CommonException\NetworkException\CurlException` on performing a request
370+
- `BasicHttpClient\Exception\HttpResponseException` if parsing the JSON response body fails
371+
372+
---
373+
299374
## Contribution
300375

301376
Contributing to our projects is always very appreciated.

Diff for: src/Response/JsonResponse.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class JsonResponse extends AbstractResponse
2020
*/
2121
protected function setBody($body)
2222
{
23-
$body = @json_decode($body);
24-
// if (is_null($body)) {
25-
// throw new HttpResponseException('Response data is no valid JSON string.');
26-
// }
23+
$body = @json_decode($body, true);
24+
if (is_null($body)) {
25+
throw new HttpResponseException('Response data is no valid JSON string.');
26+
}
2727
return parent::setBody($body);
2828
}
2929

0 commit comments

Comments
 (0)