1. Go to this page and download the library: Download sofa/http-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sofa / http-client example snippets
use Sofa\HttpClient\Factory;
// register factory in IoC with default app logger
app()->bind(Factory::class, fn () => new Factory(app()->environment('testing'), app('log')->driver()));
// then inject or resolve wherever you need
$httpClient = app(Factory::class)
->withOptions(['base_uri' => 'https://api.github.com'])
->enableRetries()
->enableLogging()
->make();
$httpClient->get('users/jarektkaczyk');
// et voila! Automatic retries in case of server errors and logging out of the box:
[2020-05-22 12:38:32] local.INFO: GET https://api.github.com/users/jarektkaczyk HTTP/1.1 200 (1351 application/json; charset=utf-8) {"request": , "response": {
"login": "jarektkaczyk",
"blog": "https://softonsofa.com",
"location": "Singapore",
...
}
}
// Raw example
$logger = new Logger('HttpLogger');
$logger->pushHandler(
new StreamHandler('/path/to/the/log/' . date('Y-m-d') . '.log'))
);
$clientFactory = new Factory($fakeRequests, $logger);
$httpClient = $clientFactory
->withOptions([
'base_uri' => 'https://api.github.com',
'headers' => [
'User-Agent' => 'My Awesome Client',
],
])
->enableRetries($retries = 3, $delayInSec = 1, $minStatus = 500)
->enableLogging($customLogFormat)
->withMiddleware($customGuzzleMiddleware)
->make();