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