PHP code example of philiprehberger / http-retry-client
1. Go to this page and download the library: Download philiprehberger/http-retry-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/ */
philiprehberger / http-retry-client example snippets
use PhilipRehberger\HttpRetry\Contracts\HttpExecutor;
use PhilipRehberger\HttpRetry\HttpRequest;
use PhilipRehberger\HttpRetry\HttpResponse;
use PhilipRehberger\HttpRetry\RetryClient;
class CurlExecutor implements HttpExecutor
{
public function execute(HttpRequest $request): HttpResponse
{
$ch = curl_init($request->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->method);
if ($request->body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->body);
}
$body = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return new HttpResponse($statusCode, $body);
}
}
$client = new RetryClient(new CurlExecutor());
$result = $client->send(new HttpRequest('GET', 'https://api.example.com/data'));
if ($result->successful()) {
echo $result->body;
}
use PhilipRehberger\HttpRetry\RetryClient;
use PhilipRehberger\HttpRetry\RetryPolicy;
$policy = new RetryPolicy(
maxRetries: 5,
baseDelayMs: 200,
maxDelayMs: 30000,
multiplier: 2.0,
jitter: true,
retryableStatusCodes: [429, 500, 502, 503, 504],
);
$client = new RetryClient($executor, $policy);