| @@ 24-43 (lines=20) @@ | ||
| 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(27) . "[0m\n"; |
|
| 28 | ||
| 29 | foreach ($request->getHeaders() as $key => $headers) { |
|
| 30 | foreach ($headers as $header) { |
|
| 31 | echo "${key}: ${header}\n"; |
|
| 32 | } |
|
| 33 | } |
|
| 34 | ||
| 35 | $body = (string) $request->getBody(); |
|
| 36 | echo $body; |
|
| 37 | $json = @json_decode($body, true); |
|
| 38 | if ($json === null) { |
|
| 39 | $json = []; |
|
| 40 | } |
|
| 41 | print_r($json); |
|
| 42 | }; |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * @return \Closure |
|
| @@ 50-70 (lines=21) @@ | ||
| 47 | */ |
|
| 48 | public static function response() |
|
| 49 | { |
|
| 50 | return function (RequestInterface $request, $options, PromiseInterface $responsePromise) { |
|
| 51 | return $responsePromise->then(function (ResponseInterface $response) { |
|
| 52 | echo chr(27) . '[33m' . "RESPONSE: HTTP/" . |
|
| 53 | $response->getProtocolVersion() . ' ' . |
|
| 54 | $response->getStatusCode() . ' ' . |
|
| 55 | $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 | ||
| 73 | /** |
|