| @@ 24-45 (lines=22) @@ | ||
| 21 | /** |
|
| 22 | * @return \Closure |
|
| 23 | */ |
|
| 24 | public static function request() |
|
| 25 | { |
|
| 26 | return function (RequestInterface $request) { |
|
| 27 | echo chr(27) . '[33m' . "REQUEST: " . $request->getMethod() . ' ' . $request->getRequestTarget() . chr( |
|
| 28 | 27 |
|
| 29 | ) . "[0m\n"; |
|
| 30 | ||
| 31 | foreach ($request->getHeaders() as $key => $headers) { |
|
| 32 | foreach ($headers as $header) { |
|
| 33 | echo "${key}: ${header}\n"; |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| 37 | $body = (string)$request->getBody(); |
|
| 38 | echo $body; |
|
| 39 | $json = @json_decode($body, true); |
|
| 40 | if ($json === null) { |
|
| 41 | $json = []; |
|
| 42 | } |
|
| 43 | print_r($json); |
|
| 44 | }; |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @return \Closure |
|
| @@ 54-69 (lines=16) @@ | ||
| 51 | { |
|
| 52 | return function (RequestInterface $request, $options, FulfilledPromise $responsePromise) { |
|
| 53 | $responsePromise->then( |
|
| 54 | function (ResponseInterface $response) { |
|
| 55 | echo chr(27) . '[33m' . "RESPONSE: HTTP/" . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . chr(27) . "[0m\n"; |
|
| 56 | ||
| 57 | foreach ($response->getHeaders() as $key => $headers) { |
|
| 58 | foreach ($headers as $header) { |
|
| 59 | echo "${key}: ${header}\n"; |
|
| 60 | } |
|
| 61 | } |
|
| 62 | ||
| 63 | $body = (string)$response->getBody(); |
|
| 64 | $json = @json_decode($body, true); |
|
| 65 | if ($json === null) { |
|
| 66 | $json = []; |
|
| 67 | } |
|
| 68 | print_r($json); |
|
| 69 | } |
|
| 70 | ); |
|
| 71 | }; |
|
| 72 | } |
|