PHP-FIG is: the PHP Interop Group. It proposes (RFC) and defines PHP Standard Recommendations (PSR). See: https://www.php-fig.org/psr/
Here are the few PSR dealt in this document:
You may have first to install PHP Composer.
Then, you will have to clone or download this repository (see clone button).
And finally, you will have to run the following command line:
$ cd path/to/psr-web-client && composer update
---
config:
theme: redux
look: neo
---
flowchart LR
classDef begin fill: #058, color:white
classDef error fill: #c00, color:white
classDef success fill: #afa, color:black
Storage[(Storage)]
RequestStream(Stream 1)
ResponseStream(Stream 2)
subgraph WebClient
Storage
sendRequest
end
Uri --> Request
RequestStream -.->|Upload| Request --> sendRequest
sendRequest -.-> ClientException
sendRequest --> Response
Response -.->|Download| ResponseStream -.-> Storage
Storage -.-> RequestStream
sendRequest <-.->|Cookies| Storage
class ClientException error
class Request begin
class Response success
Storage can either be:
- Memory if we use simple MemoryStream class
- Disk if we use FileStream class for upload(s) and/or download(s)
Respecting PSR-7 interfaces and PSR-18 interfaces, several HTTP classes were implemented in this chronological order:
- Logger,
- Response,
- Uri,
- MemoryStream (light and easy cases),
- Request,
- Client,
- FileStream (work in progress)
Simple (GET) example:
$webClient = new \Http\Client; // init web client
$request = new \Http\Request(new \Http\Uri($url)); // create the HTTP request
$response = $webClient->sendRequest($request); // <-- Exec the request. May throw ClientException
$data = $response->jsonDecode(); // It's a kind of magic...! But it may throw an exception
forEach ($data->records as $record) {
// process each record
}Here is a more complex example:
$webClient = new \Http\Client; // init web client
$webContextArr = [ // as options in: https://www.php.net/manual/en/function.stream-context-create.php
'header' => [$headerStr], // custom HTTP request header(s)
'timeout' => 5, // timeout in seconds
'method' => $method,
];
$uri = new \Http\Uri($url); // Convert URL string to Uri object
$request = new \Http\Request($uri, $webContextArr); // create the HTTP request
if (isSet($dataToSend) && is_string($dataToSend) && trim($dataToSend)!=='') {
$request = $request->withBody($dataToSend); // Can be a file to upload instead of a string (soon)
}
$response = $webClient->sendRequest($request); // <-- Exec the request. May throw ClientException
$data = $response->jsonDecode(); // It's a kind of magic...! But it may throw an exception
forEach ($data->records as $record) {
// process each record
}See also PHP manual about stream context
This kind of implementation can support very complex web requests such as sending big binary files (ZIP for examples) and also download big files as the response. It may also support even more complex HTTP requests such as video streaming.
If you find any bug, please report it by creating an issue here. Pull requests are welcomed.