Completed
Push — master ( f7d0f0...920ceb )
by Kanto
27s queued 11s
created

HttpClient::buildHeader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Kore;
3
4
use Kore\Log;
5
6
class HttpClient
7
{
8
    /**
9
     * @param string $url
10
     * @param array<mixed> $params
11
     * @param array<mixed> $headers
12
     * @param string|null $userpwd
13
     * @return mixed
14
     */
15
    public function get($url, $params = [], $headers = [], $userpwd = null)
16
    {
17
        return $this->communicate('GET', $url, $params, $headers, $userpwd);
18
    }
19
20
    /**
21
     * @param string $url
22
     * @param array<mixed> $params
23
     * @param array<mixed> $headers
24
     * @param string|null $userpwd
25
     * @return mixed
26
     */
27
    public function post($url, $params = [], $headers = [], $userpwd = null)
28
    {
29
        return $this->communicate('POST', $url, $params, $headers, $userpwd);
30
    }
31
32
    /**
33
     * @param string $url
34
     * @param array<mixed> $params
35
     * @param array<mixed> $headers
36
     * @param string|null $userpwd
37
     * @return mixed
38
     */
39
    public function put($url, $params = [], $headers = [], $userpwd = null)
40
    {
41
        return $this->communicate('PUT', $url, $params, $headers, $userpwd);
42
    }
43
44
    /**
45
     * @param string $url
46
     * @param array<mixed> $params
47
     * @param array<mixed> $headers
48
     * @param string|null $userpwd
49
     * @return mixed
50
     */
51
    public function patch($url, $params = [], $headers = [], $userpwd = null)
52
    {
53
        return $this->communicate('PATCH', $url, $params, $headers, $userpwd);
54
    }
55
56
    /**
57
     * @param string $url
58
     * @param array<mixed> $params
59
     * @param array<mixed> $headers
60
     * @param string|null $userpwd
61
     * @return mixed
62
     */
63
    public function delete($url, $params = [], $headers = [], $userpwd = null)
64
    {
65
        return $this->communicate('DELETE', $url, $params, $headers, $userpwd);
66
    }
67
68
    /**
69
     * @param string $method
70
     * @param string $url
71
     * @param array<mixed> $params
72
     * @param array<mixed> $headers
73
     * @param string|null $userpwd
74
     * @return HttpResponse|false
75
     */
76
    protected function communicate($method, $url, $params = [], $headers = [], $userpwd = null)
77
    {
78
        $headers = $this->buildHeader($headers);
79
        
80
        $curl = curl_init();
81
82
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
83
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
84
        curl_setopt($curl, CURLOPT_HEADER, true);
85
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
86
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
87
        if ($method === 'GET') {
88
            curl_setopt($curl, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : '&') . http_build_query($params));
89
        } elseif ($method === 'POST' || $method === 'PUT' || $method === 'PATCH' || $method === 'DELETE') {
90
            curl_setopt($curl, CURLOPT_URL, $url);
91
            if (count(preg_grep("/^Content-Type: application\/json/i", $headers)) > 0) {
92
                $data = json_encode($params);
93
            } else {
94
                $data = http_build_query($params);
95
            }
96
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
97
        }
98
        if (!empty($headers)) {
99
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
100
        }
101
        if ($userpwd !== null) {
102
            curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
103
        }
104
105
        $response = curl_exec($curl);
106
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
107
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
108
        $total_time = curl_getinfo($curl, CURLINFO_TOTAL_TIME);
109
110
        curl_close($curl);
111
112
        Log::debug(sprintf('[%s][%s][%ssec]', $url, $http_code, $total_time));
113
        if (!$response) {
114
            Log::info('Acquisition failed');
115
            return false;
116
        }
117
        /** @phpstan-ignore-next-line */
118
        $header = substr($response, 0, $header_size);
119
        /** @phpstan-ignore-next-line */
120
        $body = substr($response, $header_size);
121
        return new HttpResponse($http_code, $header, $body);
122
    }
123
124
    /**
125
     * @param array<mixed> $headers
126
     * @return array<string>
127
     */
128
    protected function buildHeader($headers)
129
    {
130
        $h = array();
131
        foreach ($headers as $key => $value) {
132
            if (is_string($key)) {
133
                $h[] = "$key: $value";
134
            } else {
135
                $h[] = $value;
136
            }
137
        }
138
        return $h;
139
    }
140
}
141
142
class HttpResponse
143
{
144
    /**
145
     * @param int $httpCode
146
     * @param string $header
147
     * @param string $body
148
     * @return void
149
     */
150
    public function __construct($httpCode, $header, $body)
151
    {
152
        $this->httpCode = $httpCode;
153
        $this->header = $header;
154
        $this->body = $body;
155
    }
156
157
    /**
158
     * @var int
159
     */
160
    private $httpCode;
161
    /**
162
     * @var string
163
     */
164
    private $header;
165
    /**
166
     * @var string
167
     */
168
    private $body;
169
170
    /**
171
     * @return int
172
     */
173
    public function getHttpCode()
174
    {
175
        return $this->httpCode;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getHeader()
182
    {
183
        return $this->header;
184
    }
185
186
    /**
187
     * @param string $key
188
     * @return string|null
189
     */
190
    public function getHeaderLine($key)
191
    {
192
        preg_match("/$key: (\S*)/i", $this->getHeader(), $matches);
193
        if (!isset($matches[1])) {
194
            return null;
195
        }
196
        return $matches[1];
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getBody()
203
    {
204
        return $this->body;
205
    }
206
    
207
    /**
208
     * @return array<mixed>
209
     */
210
    public function getJsonBody()
211
    {
212
        return json_decode($this->getBody(), true);
213
    }
214
}
215