Passed
Push — master ( 65fed3...c68220 )
by frey
05:44 queued 02:42
created

HttpClient::setCurlSSLVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv4\Client;
4
5
class HttpClient
6
{
7
    private static $httpInfo = '';
8
    private static $curlHandler;
9
10
    /**
11
     * send http request.
12
     *
13
     * @param array $request http请求信息
14
     *                       url        : 请求的url地址
15
     *                       method     : 请求方法,'get', 'post', 'put', 'delete', 'head'
16
     *                       data       : 请求数据,如有设置,则method为post
17
     *                       header     : 需要设置的http头部
18
     *                       host       : 请求头部host
19
     *                       timeout    : 请求超时时间
20
     *                       cert       : ca文件路径
21
     *                       ssl_version: SSL版本号
22
     *
23
     * @return string http请求响应
24
     */
25 15
    public static function sendRequest($request)
26
    {
27 15
        if (self::$curlHandler) {
28 14
            if (function_exists('curl_reset')) {
29 14
                curl_reset(self::$curlHandler);
30 14
            } else {
31
                LibcurlHelper::my_curl_reset(self::$curlHandler);
32
            }
33 14
        } else {
34 1
            self::$curlHandler = curl_init();
35
        }
36
37 15
        curl_setopt(self::$curlHandler, CURLOPT_URL, $request['url']);
38
39 15
        $method = 'GET';
40 15
        if (isset($request['method']) &&
41 15
            in_array(strtolower($request['method']), ['get', 'post', 'put', 'delete', 'head'])
42 15
        ) {
43 15
            $method = strtoupper($request['method']);
44 15
        } elseif (isset($request['data'])) {
45
            $method = 'POST';
46
        }
47
48 15
        $header = isset($request['header']) ? $request['header'] : [];
49 15
        $header[] = 'Method:'.$method;
50 15
        $header[] = 'User-Agent:'.Conf::getUserAgent();
51 15
        $header[] = 'Connection: keep-alive';
52
53 15
        isset($request['host']) && $header[] = 'Host:'.$request['host'];
54 15
        curl_setopt(self::$curlHandler, CURLOPT_RETURNTRANSFER, 1);
55 15
        curl_setopt(self::$curlHandler, CURLOPT_CUSTOMREQUEST, $method);
56 15
        isset($request['timeout']) && curl_setopt(self::$curlHandler, CURLOPT_TIMEOUT, $request['timeout']);
57
58 15
        if (isset($request['data']) && in_array($method, ['POST', 'PUT'])) {
59 8
            if (defined('CURLOPT_SAFE_UPLOAD')) {
60 8
                curl_setopt(self::$curlHandler, CURLOPT_SAFE_UPLOAD, true);
61 8
            }
62
63 8
            curl_setopt(self::$curlHandler, CURLOPT_POST, true);
64 8
            array_push($header, 'Expect: 100-continue');
65
66 8
            if (is_array($request['data'])) {
67 4
                $arr = LibcurlHelper::buildCustomPostFields($request['data']);
68 4
                array_push($header, 'Content-Type: multipart/form-data; boundary='.$arr[0]);
69 4
                curl_setopt(self::$curlHandler, CURLOPT_POSTFIELDS, $arr[1]);
70 4
            } else {
71 4
                curl_setopt(self::$curlHandler, CURLOPT_POSTFIELDS, $request['data']);
72
            }
73 8
        }
74 15
        curl_setopt(self::$curlHandler, CURLOPT_HTTPHEADER, $header);
75
76 15
        $ssl = substr($request['url'], 0, 8) == 'https://' ? true : false;
77 15
        if (isset($request['cert'])) {
78
            curl_setopt(self::$curlHandler, CURLOPT_SSL_VERIFYPEER, true);
79
            curl_setopt(self::$curlHandler, CURLOPT_CAINFO, $request['cert']);
80
            curl_setopt(self::$curlHandler, CURLOPT_SSL_VERIFYHOST, 2);
81
            self::setCurlSSLVersion($request);
82 15
        } elseif ($ssl) {
83
            curl_setopt(self::$curlHandler, CURLOPT_SSL_VERIFYPEER, false); //true any ca
84
            curl_setopt(self::$curlHandler, CURLOPT_SSL_VERIFYHOST, 1); //check only host
85
            self::setCurlSSLVersion($request);
86
        }
87 15
        $ret = curl_exec(self::$curlHandler);
88 15
        self::$httpInfo = curl_getinfo(self::$curlHandler);
89
90 15
        return $ret;
91
    }
92
93
    public static function info()
94
    {
95
        return self::$httpInfo;
96
    }
97
98
    private static function setCurlSSLVersion($request)
99
    {
100
        if (isset($request['ssl_version'])) {
101
            curl_setopt(self::$curlHandler, CURLOPT_SSLVERSION, $request['ssl_version']);
102
        } else {
103
            curl_setopt(self::$curlHandler, CURLOPT_SSLVERSION, 4);
104
        }
105
    }
106
}
107