|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* HTTP\Request |
|
5
|
|
|
* |
|
6
|
|
|
* Core\HTTP Interfaces. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015-2016 - http://caffeina.com |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace HTTP; |
|
14
|
|
|
|
|
15
|
|
|
class Request { |
|
16
|
|
|
|
|
17
|
|
|
const FORM = 0, |
|
18
|
|
|
JSON = 1; |
|
19
|
|
|
|
|
20
|
|
|
public $method = 'GET', |
|
|
|
|
|
|
21
|
|
|
$encoding = self::FORM, |
|
22
|
|
|
$url = '', |
|
23
|
|
|
$headers = [], |
|
24
|
|
|
$body = '', |
|
25
|
|
|
$UA = "Mozilla/4.0 (compatible; Core::HTTP; Windows NT 6.1)", |
|
26
|
|
|
$auth = null; |
|
27
|
|
|
|
|
28
|
|
|
public function authorization(Auth\Method $auth){ |
|
29
|
|
|
$this->auth = $auth; |
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function data($data){ |
|
34
|
|
|
switch($this->encoding){ |
|
35
|
|
|
case self::JSON: $this->body = json_encode($data); break; |
|
|
|
|
|
|
36
|
|
|
default: case self::FORM: $this->body = http_build_query($data); break; |
|
|
|
|
|
|
37
|
|
|
}; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function method($verb){ |
|
|
|
|
|
|
42
|
|
|
$this->method = strtoupper($method); |
|
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function addHeader($key, $value){ |
|
47
|
|
|
$this->headers[$key] = $value; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function __construct($method, $url, $headers=[], $data=null){ |
|
|
|
|
|
|
52
|
|
|
$this->method($method); |
|
53
|
|
|
$this->url = new URL($this->url); |
|
|
|
|
|
|
54
|
|
|
$this->headers = (array)$headers; |
|
55
|
|
|
if ($data) $this->data($data); |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function build(){ |
|
60
|
|
|
$body = ["$this->method {$this->url->path}{$this->url->query} HTTP/1.1"]; |
|
61
|
|
|
$body[] = "Host: {$this->url->host}"; |
|
62
|
|
|
|
|
63
|
|
|
// Ensure content type |
|
64
|
|
|
if (empty($this->headers["Content-Type"])) switch($this->encoding){ |
|
65
|
|
|
case self::JSON: $this->headers["Content-Type"] = "application/json"; break; |
|
|
|
|
|
|
66
|
|
|
default: case self::FORM: $this->headers["Content-Type"] = "application/x-www-form-urlencoded"; break; |
|
|
|
|
|
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
// Apply Authentication |
|
70
|
|
|
if ($this->auth) $this->auth->applyTo($this); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
foreach ($this->headers as $key => $value) { |
|
73
|
|
|
$body[] = "$key: $value"; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$body[] = ""; |
|
77
|
|
|
$body[] = $this->body; |
|
78
|
|
|
return implode("\r\n", $body); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function __toString(){ |
|
82
|
|
|
return $this->build(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.