1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CurlClient |
4
|
|
|
* |
5
|
|
|
* @filesource CurlClient.php |
6
|
|
|
* @created 21.10.2017 |
7
|
|
|
* @package chillerlan\HTTP |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\HTTP; |
14
|
|
|
|
15
|
|
|
use chillerlan\Traits\ContainerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property resource $http |
19
|
|
|
*/ |
20
|
|
|
class CurlClient extends HTTPClientAbstract{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \stdClass |
24
|
|
|
*/ |
25
|
|
|
protected $responseHeaders; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CurlClient constructor. |
29
|
|
|
* |
30
|
|
|
* @param \chillerlan\Traits\ContainerInterface $options |
31
|
|
|
* |
32
|
|
|
* @throws \chillerlan\HTTP\HTTPClientException |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ContainerInterface $options){ |
35
|
|
|
parent::__construct($options); |
36
|
|
|
|
37
|
|
View Code Duplication |
if(!isset($this->options->ca_info) || !is_file($this->options->ca_info)){ |
|
|
|
|
38
|
|
|
throw new HTTPClientException('invalid CA file'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->http = curl_init(); |
42
|
|
|
|
43
|
|
|
curl_setopt_array($this->http, [ |
44
|
|
|
CURLOPT_HEADER => false, |
45
|
|
|
CURLOPT_RETURNTRANSFER => true, |
46
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTP|CURLPROTO_HTTPS, |
47
|
|
|
CURLOPT_CAINFO => $this->options->ca_info, |
48
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
49
|
|
|
CURLOPT_SSL_VERIFYHOST => 2, |
50
|
|
|
CURLOPT_TIMEOUT => 5, |
51
|
|
|
CURLOPT_USERAGENT => $this->options->user_agent, |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
curl_setopt_array($this->http, $this->options->curl_options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @inheritdoc */ |
58
|
|
|
protected function getResponse():HTTPResponseInterface{ |
59
|
|
|
$this->responseHeaders = new \stdClass; |
60
|
|
|
|
61
|
|
|
$headers = $this->normalizeRequestHeaders($this->requestHeaders); |
62
|
|
|
|
63
|
|
|
if(in_array($this->requestMethod, ['PATCH', 'POST', 'PUT', 'DELETE'])){ |
64
|
|
|
|
65
|
|
|
$options = in_array($this->requestMethod, ['PATCH', 'PUT', 'DELETE']) |
66
|
|
|
? [CURLOPT_CUSTOMREQUEST => $this->requestMethod] |
67
|
|
|
: [CURLOPT_POST => true]; |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
if(!isset($headers['Content-type']) && $this->requestMethod === 'POST' && is_array($this->requestBody)){ |
71
|
|
|
$headers += ['Content-type: application/x-www-form-urlencoded']; |
72
|
|
|
$this->requestBody = http_build_query($this->requestBody, '', '&', PHP_QUERY_RFC1738); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$options += [CURLOPT_POSTFIELDS => $this->requestBody]; |
76
|
|
|
} |
77
|
|
|
else{ |
78
|
|
|
$options = [CURLOPT_CUSTOMREQUEST => $this->requestMethod]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$headers += [ |
82
|
|
|
'Host: '.$this->parsedURL['host'], |
83
|
|
|
'Connection: close', |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$url = $this->requestURL.(!empty($this->requestParams) ? '?'.$this->buildQuery($this->requestParams) : ''); |
87
|
|
|
|
88
|
|
|
$options += [ |
89
|
|
|
CURLOPT_URL => $url, |
90
|
|
|
CURLOPT_HTTPHEADER => $headers, |
91
|
|
|
CURLOPT_HEADERFUNCTION => [$this, 'headerLine'], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
curl_setopt_array($this->http, $options); |
95
|
|
|
|
96
|
|
|
$response = curl_exec($this->http); |
97
|
|
|
$curl_info = curl_getinfo($this->http); |
98
|
|
|
|
99
|
|
|
curl_close($this->http); |
100
|
|
|
|
101
|
|
|
return new HTTPResponse([ |
102
|
|
|
'url' => $url, |
103
|
|
|
'curl_info' => $curl_info, |
104
|
|
|
'headers' => $this->responseHeaders, |
105
|
|
|
'body' => $response, |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param resource $curl |
111
|
|
|
* @param string $header_line |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
* |
115
|
|
|
* @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
116
|
|
|
*/ |
117
|
|
|
protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
118
|
|
|
$header = explode(':', $header_line, 2); |
119
|
|
|
|
120
|
|
|
if(count($header) === 2){ |
121
|
|
|
$this->responseHeaders->{trim(strtolower($header[0]))} = trim($header[1]); |
122
|
|
|
} |
123
|
|
|
elseif(substr($header_line, 0, 4) === 'HTTP'){ |
124
|
|
|
$status = explode(' ', $header_line, 3); |
125
|
|
|
|
126
|
|
|
$this->responseHeaders->httpversion = explode('/', $status[0], 2)[1]; |
127
|
|
|
$this->responseHeaders->statuscode = intval($status[1]); |
128
|
|
|
$this->responseHeaders->statustext = trim($status[2]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return strlen($header_line); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.