From 0cf4b279a0282c935a48b6fdb023bc886799c122 Mon Sep 17 00:00:00 2001 From: Niels Theen Date: Sun, 23 Apr 2017 02:19:49 +0200 Subject: [PATCH] Update docs and add example --- README.md | 37 ++++++++++++++++++- examples/02-client-ip.php | 25 +++++++++++++ ...unt-visitors.php => 03-count-visitors.php} | 0 ...am-response.php => 04-stream-response.php} | 0 ...ream-request.php => 05-stream-request.php} | 0 ...ror-handling.php => 06-error-handling.php} | 0 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 examples/02-client-ip.php rename examples/{02-count-visitors.php => 03-count-visitors.php} (100%) rename examples/{03-stream-response.php => 04-stream-response.php} (100%) rename examples/{04-stream-request.php => 05-stream-request.php} (100%) rename examples/{05-error-handling.php => 06-error-handling.php} (100%) diff --git a/README.md b/README.md index d8155817..2d45d6bd 100644 --- a/README.md +++ b/README.md @@ -155,12 +155,47 @@ $http = new Server($socket, function (ServerRequestInterface $request) { }); ``` +The `getServerParams(): mixed[]` method can be used to +get server-side parameters similar to the `$_SERVER` variable. +The following parameters are currently available: + +* `remote_address` + The IP address of the request sender +* `remote_port` + Port of the request sender +* `server_address` + The IP address of the server +* `server_port` + The port of the server +* `request_time` + Unix timestamp when the complete request header has been received, + as integer similar to `time()` +* `request_time_float` + Unix timestamp when the complete request header has been received, + as float similar to `microtime(true)` +* `https` + Set to 'on' if the request used HTTPS, otherwise it will be set to `null` + +```php +$http = new Server($socket, function (ServerRequestInterface $request) { + $body = "Your IP is: " . $request->getServerParams()['remote_address']; + + return new Response( + 200, + array('Content-Type' => 'text/plain'), + $body + ); +}); +``` + +See also [example #2](examples). + For more details about the request object, check out the documentation of [PSR-7 ServerRequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface) and [PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface). -> Currently the the server params, cookies and uploaded files are not added by the +> Currently the the cookies and uploaded files are not added by the `Server`, but you can add these parameters by yourself using the given methods. The next versions of this project will cover these features. diff --git a/examples/02-client-ip.php b/examples/02-client-ip.php new file mode 100644 index 00000000..97cadd61 --- /dev/null +++ b/examples/02-client-ip.php @@ -0,0 +1,25 @@ +getServerParams()['remote_address']; + + return new Response( + 200, + array('Content-Type' => 'text/plain'), + $body + ); +}); + +echo 'Listening on http://' . $socket->getAddress() . PHP_EOL; + +$loop->run(); diff --git a/examples/02-count-visitors.php b/examples/03-count-visitors.php similarity index 100% rename from examples/02-count-visitors.php rename to examples/03-count-visitors.php diff --git a/examples/03-stream-response.php b/examples/04-stream-response.php similarity index 100% rename from examples/03-stream-response.php rename to examples/04-stream-response.php diff --git a/examples/04-stream-request.php b/examples/05-stream-request.php similarity index 100% rename from examples/04-stream-request.php rename to examples/05-stream-request.php diff --git a/examples/05-error-handling.php b/examples/06-error-handling.php similarity index 100% rename from examples/05-error-handling.php rename to examples/06-error-handling.php