Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <j.boggiano@seld.be>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Monolog\Handler\Curl;
13
 
14
class Util
15
{
16
    private static $retriableErrorCodes = array(
17
        CURLE_COULDNT_RESOLVE_HOST,
18
        CURLE_COULDNT_CONNECT,
19
        CURLE_HTTP_NOT_FOUND,
20
        CURLE_READ_ERROR,
21
        CURLE_OPERATION_TIMEOUTED,
22
        CURLE_HTTP_POST_ERROR,
23
        CURLE_SSL_CONNECT_ERROR,
24
    );
25
 
26
    /**
27
     * Executes a CURL request with optional retries and exception on failure
28
     *
29
     * @param  resource          $ch curl handler
30
     * @throws \RuntimeException
31
     */
32
    public static function execute($ch, $retries = 5, $closeAfterDone = true)
33
    {
34
        while ($retries--) {
35
            if (curl_exec($ch) === false) {
36
                $curlErrno = curl_errno($ch);
37
 
38
                if (false === in_array($curlErrno, self::$retriableErrorCodes, true) || !$retries) {
39
                    $curlError = curl_error($ch);
40
 
41
                    if ($closeAfterDone) {
42
                        curl_close($ch);
43
                    }
44
 
45
                    throw new \RuntimeException(sprintf('Curl error (code %s): %s', $curlErrno, $curlError));
46
                }
47
 
48
                continue;
49
            }
50
 
51
            if ($closeAfterDone) {
52
                curl_close($ch);
53
            }
54
            break;
55
        }
56
    }
57
}