Skip to content

Commit 12661cf

Browse files
committed
Fix php-curl-class#581: Add example to retry request based on HTTP status code.
1 parent 244465b commit 12661cf

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require __DIR__ . '/../vendor/autoload.php';
3+
4+
use \Curl\Curl;
5+
6+
$max_retries = 3;
7+
8+
$curl = new Curl();
9+
$curl->setRetry(function ($instance) use ($max_retries) {
10+
// Retry when the result of curl_getinfo($instance->curl, CURLINFO_HTTP_CODE) is 500, 503.
11+
return $instance->retries < $max_retries && in_array($instance->httpStatusCode, array(500, 503));
12+
});
13+
$curl->get('https://httpbin.org/status/503');
14+
15+
if ($curl->error) {
16+
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
17+
echo 'attempts: ' . $curl->attempts . "\n";
18+
echo 'retries: ' . $curl->retries . "\n";
19+
} else {
20+
echo 'Response:' . "\n";
21+
var_dump($curl->response);
22+
}

0 commit comments

Comments
 (0)