slepic / http-transfer
Simple PHP library working with PSR HTTP message transfers.
Installs: 18 035
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/slepic/http-transfer
Requires
- php: >=8.0
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpunit/phpunit: ^9
Suggests
- slepic/http-transfer-log-consumer: See various log consumers.
- slepic/http-transfer-observer-consumer: See observer consumers, possibly adapters of various http clients.
- slepic/http-transfer-observer-implementation: See various observer implementations.
Provides
This package is auto-updated.
Last update: 2025-10-08 02:23:18 UTC
README
http-transfer
Simple PHP library working with PSR HTTP message transfers.
Usage
There are 3 components at this moment:
Log
This component consists of:
- interface Slepic\Http\Transfer\Log\LogInterfacedefining a simple structure holing the request, response, error, time of start and end of the transfer.- and its implementation Slepic\Http\Transfer\Log\Log
 
- and its implementation 
- interface Slepic\Http\Transfer\Log\StorageInterfacewhich is used to store the logs.- and of course its simple implementation Slepic\Http\Transfer\Log\ArrayStoragewhich stores the logs in array.
 
- and of course its simple implementation 
$storage = new ArrayStorage();
$storage->store(new Log($request, $start, $end, $response, $exception, $context));
assert($storage[0]->getRequest() === $request);
assert($storage[0]->getResponse() === $response);
assert($storage[0]->getException() === $exception);
assert($storage[0]->getStartTime() === $start);
assert($storage[0]->getEndTime() === $end);
assert($storage[0]->getContext() === $context);
Observer
An abstraction over the transfer process where the observer is notified about start and end of transfer processes.
The observation is provided by the Slepic\Http\Transfer\Observer\ObserverInterface which is in fact just a factory.
The observer takes the initiating request and returns a one use object implementing Slepic\Http\Transfer\Observer\ObserverDelegateInterface.
The delegate is destined to be notified of either success or failure of the transfer, and then it gets destroyed by garbage collector.
$observer = new SomeImplementationOfObserverInterface();
$delegate = $observer->observe($request, $context);
//process the $request
//...
//got $response ...
$delegate->success($response);
//or maybe got network exception
$delegate->error($exception);
//or maybe some client like guzzle throw exceptions for HTTP 4xx and 5xx when the response object exists along the exception
$delegate->error($exception, $exception instanceof GuzzleException ? $exception->getResponse() : null);
History
A coposition of the two above that implements the osbserver to create the logs with duration information.
This one contains just an implementation of the Slepic\Http\Transfer\Observer\ObserverInterface.
The Slepic\Http\Transfer\History\HistoryObserver pushes transfer logs to a storage as they get completed.
Well of course the job is actualy done by its delegate Slepic\Http\Transfer\History\HistoryObserverDelegate
//create services
$storage = new ArrayStorage();
$observer = new HistoryObserver($storage);
//somewhere you send some requests
foreach ($requests as $request) {his 
  $delegate = $observer->observe($request);
  try {
    $response = $client->sendRequest($request);
  } catch (\Exception $e) {
    $delegate->error($e);
    throw $e;
  }
  $delegate->success($response);
}
//and when u need it you can access transfer logs
foreach ($storage as $log) {
  var_dump($log->getRequest());
  var_dump($log->getResponse());
  var_dump($log->getEndTime() - $log->getStartTime());
  //...
}
Packagist Providers
- slepic/http-transfer-log-consumer- consumers of LogInterface
- slepic/http-transfer-observer-consumer- consumers of ObserverInterface and ObserverDelegateInterface
- slepic/http-transfer-observer-implementation- implementations of ObserverInterface and ObserverDelegateInterface.
If you use this library in a public project and you use composer and share the project on packagist, consider adding the packagist providers above to your composer.json like this:
"provide": {
  "slepic/http-transfer-*-consumer": "*", //if you created a consumer of correspoding interface(s)
  "slepic/http-transfer-*-implementation": "*" //if you created implementation of corresponding interface(s)
},
"suggest": {
  "slepic/http-transfer-*-consumer": "*",  //if you created implementation of corresponding interface(s)
  "slepic/http-transfer-*-implementation": "*" //if you created a consumer of correspoding interface(s)
}