Completed
Push — master ( 464f3e...afe7d8 )
by r
13s queued 10s
created

Client::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Http;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Request;
6
use Qiniu\Http\Response;
7
8
final class Client
9
{
10 69
    public static function get($url, array $headers = array())
11
    {
12 69
        $request = new Request('GET', $url, $headers);
13 69
        return self::sendRequest($request);
14
    }
15
16
    public static function delete($url, array $headers = array())
17
    {
18
        $request = new Request('DELETE', $url, $headers);
19
        return self::sendRequest($request);
20
    }
21
22 57
    public static function post($url, $body, array $headers = array())
23
    {
24 57
        $request = new Request('POST', $url, $headers, $body);
25 57
        return self::sendRequest($request);
26
    }
27
28
    public static function PUT($url, $body, array $headers = array())
29
    {
30
        $request = new Request('PUT', $url, $headers, $body);
31
        return self::sendRequest($request);
32
    }
33
34 9
    public static function multipartPost(
35
        $url,
36
        $fields,
37
        $name,
38
        $fileName,
39
        $fileBody,
40
        $mimeType = null,
41
        array $headers = array()
42
    ) {
43 9
        $data = array();
44 9
        $mimeBoundary = md5(microtime());
45
46 9
        foreach ($fields as $key => $val) {
47 9
            array_push($data, '--' . $mimeBoundary);
48 9
            array_push($data, "Content-Disposition: form-data; name=\"$key\"");
49 9
            array_push($data, '');
50 9
            array_push($data, $val);
51 9
        }
52
53 9
        array_push($data, '--' . $mimeBoundary);
54 9
        $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
55 9
        $finalFileName = self::escapeQuotes($fileName);
56 9
        array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
57 9
        array_push($data, "Content-Type: $finalMimeType");
58 9
        array_push($data, '');
59 9
        array_push($data, $fileBody);
60
61 9
        array_push($data, '--' . $mimeBoundary . '--');
62 9
        array_push($data, '');
63
64 9
        $body = implode("\r\n", $data);
65
        // var_dump($data);exit;
66 9
        $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
67 9
        $headers['Content-Type'] = $contentType;
68 9
        $request = new Request('POST', $url, $headers, $body);
69 9
        return self::sendRequest($request);
70
    }
71
72 102
    private static function userAgent()
73
    {
74 102
        $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
75
76 102
        $systemInfo = php_uname("s");
77 102
        $machineInfo = php_uname("m");
78
79 102
        $envInfo = "($systemInfo/$machineInfo)";
80
81 102
        $phpVer = phpversion();
82
83 102
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
84 102
        return $ua;
85
    }
86
87 102
    public static function sendRequest($request)
88
    {
89 102
        $t1 = microtime(true);
90 102
        $ch = curl_init();
91
        $options = array(
92 102
            CURLOPT_USERAGENT => self::userAgent(),
93 102
            CURLOPT_RETURNTRANSFER => true,
94 102
            CURLOPT_SSL_VERIFYPEER => false,
95 102
            CURLOPT_SSL_VERIFYHOST => false,
96 102
            CURLOPT_HEADER => true,
97 102
            CURLOPT_NOBODY => false,
98 102
            CURLOPT_CUSTOMREQUEST => $request->method,
99 102
            CURLOPT_URL => $request->url,
100 102
        );
101
        // Handle open_basedir & safe mode
102 102
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
103 102
            $options[CURLOPT_FOLLOWLOCATION] = true;
104 102
        }
105 102
        if (!empty($request->headers)) {
106 69
            $headers = array();
107 69
            foreach ($request->headers as $key => $val) {
108 69
                array_push($headers, "$key: $val");
109 69
            }
110 69
            $options[CURLOPT_HTTPHEADER] = $headers;
111 69
        }
112 102
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
113 102
        if (!empty($request->body)) {
114 39
            $options[CURLOPT_POSTFIELDS] = $request->body;
115 39
        }
116 102
        curl_setopt_array($ch, $options);
117 102
        $result = curl_exec($ch);
118 102
        $t2 = microtime(true);
119 102
        $duration = round($t2 - $t1, 3);
120 102
        $ret = curl_errno($ch);
121 102
        if ($ret !== 0) {
122 3
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
123 3
            curl_close($ch);
124 3
            return $r;
125
        }
126 102
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
127 102
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
128 102
        $headers = self::parseHeaders(substr($result, 0, $header_size));
129 102
        $body = substr($result, $header_size);
130 102
        curl_close($ch);
131 102
        return new Response($code, $duration, $headers, $body, null);
132
    }
133
134 102
    private static function parseHeaders($raw)
135
    {
136 102
        $headers = array();
137 102
        $headerLines = explode("\r\n", $raw);
138 102
        foreach ($headerLines as $line) {
139 102
            $headerLine = trim($line);
140 102
            $kv = explode(':', $headerLine);
141 102
            if (count($kv) > 1) {
142 102
                $kv[0] =self::ucwordsHyphen($kv[0]);
143 102
                $headers[$kv[0]] = trim($kv[1]);
144 102
            }
145 102
        }
146 102
        return $headers;
147
    }
148
149 9
    private static function escapeQuotes($str)
150
    {
151 9
        $find = array("\\", "\"");
152 9
        $replace = array("\\\\", "\\\"");
153 9
        return str_replace($find, $replace, $str);
154
    }
155
    
156 102
    private static function ucwordsHyphen($str)
157
    {
158 102
        return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
159
    }
160
}
161