1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait HTTPClientTrait |
4
|
|
|
* |
5
|
|
|
* @filesource HTTPClientTrait.php |
6
|
|
|
* @created 28.01.2018 |
7
|
|
|
* @package chillerlan\HTTP |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2018 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\HTTP; |
14
|
|
|
|
15
|
|
|
trait HTTPClientTrait{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \chillerlan\HTTP\HTTPClientInterface |
19
|
|
|
*/ |
20
|
|
|
private $http; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \chillerlan\HTTP\HTTPClientInterface $http |
24
|
|
|
* |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
protected function setHTTPClient(HTTPClientInterface $http){ |
28
|
|
|
$this->http = $http; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $url |
35
|
|
|
* @param array|null $params |
36
|
|
|
* @param string|null $method |
37
|
|
|
* @param null $body |
38
|
|
|
* @param array|null $headers |
39
|
|
|
* |
40
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
41
|
|
|
*/ |
42
|
|
|
protected function httpRequest(string $url, array $params = null, string $method = null, $body = null, array $headers = null):HTTPResponseInterface{ |
43
|
|
|
return $this->http->request($url, $params, $method, $body, $headers); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $url |
48
|
|
|
* @param array|null $params |
49
|
|
|
* @param array|null $headers |
50
|
|
|
* |
51
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
52
|
|
|
*/ |
53
|
|
|
protected function httpDELETE(string $url, array $params = null, array $headers = null):HTTPResponseInterface{ |
54
|
|
|
return $this->http->request($url, $params, 'DELETE', null, $headers); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $url |
59
|
|
|
* @param array|null $params |
60
|
|
|
* @param array|null $headers |
61
|
|
|
* |
62
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
63
|
|
|
*/ |
64
|
|
|
protected function httpGET(string $url, array $params = null, array $headers = null):HTTPResponseInterface{ |
65
|
|
|
return $this->http->request($url, $params, 'GET', null, $headers); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $url |
70
|
|
|
* @param array|null $params |
71
|
|
|
* @param null $body |
72
|
|
|
* @param array|null $headers |
73
|
|
|
* |
74
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
75
|
|
|
*/ |
76
|
|
|
protected function httpPATCH(string $url, array $params = null, $body = null, array $headers = null):HTTPResponseInterface{ |
77
|
|
|
return $this->http->request($url, $params, 'PATCH', $body, $headers); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $url |
82
|
|
|
* @param array|null $params |
83
|
|
|
* @param null $body |
84
|
|
|
* @param array|null $headers |
85
|
|
|
* |
86
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
87
|
|
|
*/ |
88
|
|
|
protected function httpPOST(string $url, array $params = null, $body = null, array $headers = null):HTTPResponseInterface{ |
89
|
|
|
return $this->http->request($url, $params, 'POST', $body, $headers); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $url |
94
|
|
|
* @param array|null $params |
95
|
|
|
* @param null $body |
96
|
|
|
* @param array|null $headers |
97
|
|
|
* |
98
|
|
|
* @return \chillerlan\HTTP\HTTPResponseInterface |
99
|
|
|
*/ |
100
|
|
|
protected function httpPUT(string $url, array $params = null, $body = null, array $headers = null):HTTPResponseInterface{ |
101
|
|
|
return $this->http->request($url, $params, 'PUT', $body, $headers); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $params |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected function checkParams($params){ |
110
|
|
|
|
111
|
|
|
if(is_array($params)){ |
112
|
|
|
|
113
|
|
|
foreach($params as $key => $value){ |
114
|
|
|
|
115
|
|
|
if(is_bool($value)){ |
116
|
|
|
$params[$key] = (string)(int)$value; |
117
|
|
|
} |
118
|
|
|
elseif(is_null($value) || empty($value)){ |
119
|
|
|
unset($params[$key]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $params; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $data |
131
|
|
|
* |
132
|
|
|
* @return array|string |
133
|
|
|
*/ |
134
|
|
|
protected function rawurlencode($data){ |
135
|
|
|
|
136
|
|
|
if(is_array($data)){ |
137
|
|
|
return array_map([$this, 'rawurlencode'], $data); |
138
|
|
|
} |
139
|
|
|
elseif(is_scalar($data)){ |
140
|
|
|
return rawurlencode($data); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $data; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* from https://github.com/abraham/twitteroauth/blob/master/src/Util.php |
148
|
|
|
* |
149
|
|
|
* @param array $params |
150
|
|
|
* @param bool $urlencode |
|
|
|
|
151
|
|
|
* @param string $delimiter |
|
|
|
|
152
|
|
|
* @param string $enclosure |
|
|
|
|
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function buildHttpQuery(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null):string { |
157
|
|
|
|
158
|
|
|
if(empty($params)) { |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// urlencode both keys and values |
163
|
|
|
if($urlencode ?? true){ |
164
|
|
|
$params = array_combine( |
165
|
|
|
$this->rawurlencode(array_keys($params)), |
166
|
|
|
$this->rawurlencode(array_values($params)) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Parameters are sorted by name, using lexicographical byte value ordering. |
171
|
|
|
// Ref: Spec: 9.1.1 (1) |
|
|
|
|
172
|
|
|
uksort($params, 'strcmp'); |
173
|
|
|
|
174
|
|
|
$pairs = []; |
175
|
|
|
$enclosure = $enclosure ?? ''; |
176
|
|
|
|
177
|
|
|
foreach($params as $parameter => $value){ |
178
|
|
|
|
179
|
|
|
if(is_array($value)) { |
180
|
|
|
// If two or more parameters share the same name, they are sorted by their value |
181
|
|
|
// Ref: Spec: 9.1.1 (1) |
|
|
|
|
182
|
|
|
// June 12th, 2010 - changed to sort because of issue 164 by hidetaka |
183
|
|
|
sort($value, SORT_STRING); |
184
|
|
|
|
185
|
|
|
foreach ($value as $duplicateValue) { |
186
|
|
|
$pairs[] = $parameter.'='.$enclosure.$duplicateValue.$enclosure; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
else{ |
191
|
|
|
$pairs[] = $parameter.'='.$enclosure.$value.$enclosure; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61) |
197
|
|
|
// Each name-value pair is separated by an '&' character (ASCII code 38) |
198
|
|
|
return implode($delimiter ?? '&', $pairs); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.