Skip to content

Web client compliant with PHP Standard Recommendation #18 (PSR-18)

License

Notifications You must be signed in to change notification settings

PixEye/psr-web-client

Repository files navigation

PHP standard web client

PHP Standard Recommendations

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:

Installation

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

Diagram

---
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
Loading

Storage can either be:

  • Memory if we use simple MemoryStream class
  • Disk if we use FileStream class for upload(s) and/or download(s)

Our implementation of PSR-18

Respecting PSR-7 interfaces and PSR-18 interfaces, several HTTP classes were implemented in this chronological order:

  1. Logger,
  2. Response,
  3. Uri,
  4. MemoryStream (light and easy cases),
  5. Request,
  6. Client,
  7. FileStream (work in progress)

Use cases

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.

Bug reports

If you find any bug, please report it by creating an issue here. Pull requests are welcomed.

About

Web client compliant with PHP Standard Recommendation #18 (PSR-18)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages