CurlHttpClient.php
1 year ago
HttpClientInterface.php
1 year ago
Request.php
1 year ago
Response.php
1 year ago
Request.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Net; |
| 4 | |
| 5 | if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access |
| 6 | |
| 7 | /** |
| 8 | * Represents an HTTP request. |
| 9 | */ |
| 10 | class Request extends \WonderPush\Obj\BaseObject { |
| 11 | |
| 12 | /** |
| 13 | * HTTP GET method. |
| 14 | */ |
| 15 | const GET = 'GET'; |
| 16 | |
| 17 | /** |
| 18 | * HTTP POST method. |
| 19 | */ |
| 20 | const POST = 'POST'; |
| 21 | |
| 22 | /** |
| 23 | * HTTP PUT method. |
| 24 | */ |
| 25 | const PUT = 'PUT'; |
| 26 | |
| 27 | /** |
| 28 | * HTTP DELETE method. |
| 29 | */ |
| 30 | const DELETE = 'DELETE'; |
| 31 | |
| 32 | /** |
| 33 | * HTTP PATCH method. |
| 34 | */ |
| 35 | const PATCH = 'PATCH'; |
| 36 | |
| 37 | /** |
| 38 | * HTTP method. |
| 39 | * @var string One of the constants of this class. |
| 40 | */ |
| 41 | private $method; |
| 42 | |
| 43 | /** |
| 44 | * Scheme, host, port and potential path prefix of the request. |
| 45 | * |
| 46 | * Should not end with a slash. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | private $root; |
| 51 | |
| 52 | /** |
| 53 | * HTTP path. |
| 54 | * |
| 55 | * Should start with a slash. |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
| 59 | private $path; |
| 60 | |
| 61 | /** |
| 62 | * Query string parameters. |
| 63 | * |
| 64 | * These parameters will not be promoted to body parameters. |
| 65 | * |
| 66 | * @var mixed[] |
| 67 | */ |
| 68 | private $qsParams; |
| 69 | |
| 70 | /** |
| 71 | * Body parameters. |
| 72 | * |
| 73 | * These parameters may be promoted to query string parameters, depending on the HTTP method used. |
| 74 | * |
| 75 | * @var mixed[] |
| 76 | */ |
| 77 | private $params; |
| 78 | |
| 79 | /** |
| 80 | * An associative array of 'paramName' => ['tmp_name' => '/full/path','name' => 'filename.png', 'type' => 'image/png']. |
| 81 | * @var mixed |
| 82 | */ |
| 83 | private $files = array(); |
| 84 | |
| 85 | /** |
| 86 | * HTTP headers. |
| 87 | * @var mixed[] |
| 88 | */ |
| 89 | private $headers; |
| 90 | |
| 91 | /** |
| 92 | * The HTTP method. |
| 93 | * @return string One of the constants of this class. |
| 94 | */ |
| 95 | public function getMethod() { |
| 96 | return $this->method; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set the HTTP method. |
| 101 | * @param string $method One of the constants of this class. |
| 102 | * @return self |
| 103 | */ |
| 104 | public function setMethod($method) { |
| 105 | $this->method = $method; |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * The scheme, host, port and potential path prefix of the request. |
| 111 | * |
| 112 | * Should not end with a slash. |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | public function getRoot() { |
| 117 | return $this->root; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set the scheme, host, port and potential path prefix of the request. |
| 122 | * |
| 123 | * Should not end with a slash. |
| 124 | * |
| 125 | * @param string $root |
| 126 | * @return $this |
| 127 | */ |
| 128 | public function setRoot($root) { |
| 129 | $this->root = $root; |
| 130 | return $this; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * The HTTP path. |
| 135 | * |
| 136 | * Should start with a slash. |
| 137 | * |
| 138 | * @return string |
| 139 | */ |
| 140 | public function getPath() { |
| 141 | return $this->path; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set the HTTP path. |
| 146 | * |
| 147 | * Should start with a slash. |
| 148 | * |
| 149 | * @param string $path |
| 150 | * @return $this |
| 151 | */ |
| 152 | public function setPath($path) { |
| 153 | $this->path = $path; |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * The query string parameters. |
| 159 | * |
| 160 | * These parameters will not be promoted to body parameters. |
| 161 | * |
| 162 | * @return mixed[] |
| 163 | */ |
| 164 | public function getQsParams() { |
| 165 | return $this->qsParams; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set the query string parameters. |
| 170 | * |
| 171 | * These parameters will not be promoted to body parameters. |
| 172 | * |
| 173 | * @param mixed[] $qsParams |
| 174 | * @return $this |
| 175 | */ |
| 176 | public function setQsParams($qsParams) { |
| 177 | $this->qsParams = $qsParams; |
| 178 | return $this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param $key |
| 183 | * @param $value |
| 184 | * @return $this |
| 185 | */ |
| 186 | public function setQsParam($key, $value) { |
| 187 | if ($key === null) return $this; |
| 188 | if (!$this->qsParams) { |
| 189 | $this->qsParams = array(); |
| 190 | } |
| 191 | if ($value === null) { |
| 192 | unset($this->qsParams[$key]); |
| 193 | } else { |
| 194 | $this->qsParams[$key] = $value; |
| 195 | } |
| 196 | return $this; |
| 197 | } |
| 198 | |
| 199 | public function addFile($paramName, $filename, $path, $type) { |
| 200 | $this->files[$paramName] = array( |
| 201 | 'name' => $filename, |
| 202 | 'tmp_name' => $path, |
| 203 | 'type' => $type, |
| 204 | ); |
| 205 | return $this; |
| 206 | } |
| 207 | |
| 208 | public function removeFile($paramName) { |
| 209 | unset($this->files[$paramName]); |
| 210 | return $this; |
| 211 | } |
| 212 | |
| 213 | public function getFiles() { |
| 214 | return $this->files; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * The body parameters. |
| 219 | * |
| 220 | * These parameters may be promoted to query string parameters, depending on the HTTP method used. |
| 221 | * |
| 222 | * @return mixed[] |
| 223 | */ |
| 224 | public function getParams() { |
| 225 | return $this->params; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Set the body parameters. |
| 230 | * |
| 231 | * These parameters may be promoted to query string parameters, depending on the HTTP method used. |
| 232 | * |
| 233 | * @param mixed[] $params |
| 234 | * @return $this |
| 235 | */ |
| 236 | public function setParams($params) { |
| 237 | $this->params = $params; |
| 238 | return $this; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * The HTTP headers. |
| 243 | * @return mixed[] |
| 244 | */ |
| 245 | public function getHeaders() { |
| 246 | return $this->headers; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Set the HTTP headers. |
| 251 | * @param mixed[] $headers |
| 252 | * @return $this |
| 253 | */ |
| 254 | public function setHeaders($headers) { |
| 255 | $this->headers = $headers; |
| 256 | return $this; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @param $key |
| 261 | * @param $value |
| 262 | * @return $this |
| 263 | */ |
| 264 | public function setHeader($key, $value) { |
| 265 | if ($key === null) return $this; |
| 266 | if (!$this->headers) { |
| 267 | $this->headers = array(); |
| 268 | } |
| 269 | if ($value === null) { |
| 270 | unset($this->headers[$key]); |
| 271 | } else { |
| 272 | $this->headers[$key] = $value; |
| 273 | } |
| 274 | return $this; |
| 275 | } |
| 276 | } |
| 277 |