This library is a PSR HTTP implementation created for Shieldon firewall 2, following up the PSR (PHP Standard Recommendation) documents by one hundred percent.
- PSR-7 (HTTP Message Interfaces)
- PSR-15 (HTTP Server Request Handlers)
- PSR-17 (HTTP Factories)
GitHub Repo
https://github.com/terrylinooo/psr-http
Test Status
Shiledon PSR-HTTP library is strictly tested by unit tests contain almost all conditions that might be happened, if you find any bug or something that can improve this library, please let me know.
Test suite | Status |
---|---|
Repository built-in tests | |
PSR-7 integration tests |
You can use it on any framework which is compatible with those PSRs.
Install
composer require shieldon/psr-http
Test
composer install
composer test
Quick Start
The simplest way to start implementing PSR-7 on your PHP applications, let's check out the examples below.
Create a server request.
$serverRequest = ServerRequestFactory::fromGlobal();
Create a request.
$request = RequestFactory::fromNew();
Create a server response
$response = ResponseFactory::fromNew();
Create a URI.
// Create a URI contains visitor's information.
/**
* Assume a visior is viewing your website page,
* for example, https://yoursite/en/post1.html
*/
$uri = UriFactory::fromGlobal();
echo $uri->getPath();
// Outputs: /en/post1.html
// Create a URI just a new instance.
$uri = UriFactory::fromNew();
echo $uri->getPath();
// Outputs:
Create a stream instance.
// Create a stream just a new instance.
$stream = StreamFactory::fromNew();
Create an array with UploadedFile structure.
// Let's see the following example,
// assume we have a superglobal $_FILES looks like this.
$_FILES = [
'foo' => [
'name' => 'example1.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/php200A.tmp',
'error' => 0,
'size' => 100000,
]
];
$uploadFileArr = UploadedFileFactory::fromGlobal();
echo $uploadFileArr['foo']->getClientFilename();
// Outputs: example1.jpg
Table of Contents
-
PSR-17: HTTP Factories
-
RequestFactory
- createRequest
- ::fromNew
(Non-PSR)
-
ServerRequestFactory
- createServerRequest
- ::fromGlobal
(Non-PSR)
-
ResponseFactory
- createResponse
- ::fromNew
(Non-PSR)
-
StreamFactory
-
UploadedFileFactory
- createUploadedFile
- ::fromGlobal
(Non-PSR)
-
UriFactory
- createUri
- ::fromGlobal
(Non-PSR)
- ::fromNew
(Non-PSR)
-
-
PSR-7: HTTP Message Interfaces
-
Message
-
Request (externds Message)
-
ServerRequest (externds Request)
-
Response (externds Message)
- __construct
(Non-PSR)
- getStatusCode
- withStatus
- getReasonPhrase
- __construct
-
Stream
-
UploadedFile
- __construct
(Non-PSR)
- getStream
- moveTo
- getSize
- getError
- getClientFilename
- getClientMediaType
- getErrorMessage
(Non-PSR)
- __construct
-
Uri
-
-
PSR-15: HTTP Server Request Handlers
-
RequestHandler
- __construct
(Non-PSR)
- add
- handle
- __construct
-
Middleware
-
If you are looking for combined examples, see unit testing.
Author
Shieldon PSR HTTP library is brought to you by Terry L. from Taiwan.
License
MIT