1
|
|
|
<?php |
2
|
|
|
namespace Redbox\Twitch\Transport; |
3
|
|
|
|
4
|
|
|
class HttpRequest |
5
|
|
|
{ |
6
|
|
|
const REQUEST_METHOD_GET = 'GET'; |
7
|
|
|
const REQUEST_METHOD_POST = 'POST'; |
8
|
|
|
const REQUEST_METHOD_PUT = 'PUT'; |
9
|
|
|
const REQUEST_EXPECTED_RESPONSE_TYPE_JSON = 'JSON'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $httpResponseCode; |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $responseHeaders; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $responseBody; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $requestMethod; |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $requestHeaders; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $expectedType; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $postBody; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $url; |
45
|
|
|
|
46
|
|
|
public function __construct($url = '', $method = 'GET', $headers = array(), $post_body = array(), $expected_type = self::REQUEST_EXPECTED_RESPONSE_TYPE_JSON) |
47
|
|
|
{ |
48
|
|
|
$this->setUrl($url); |
49
|
|
|
$this->setRequestMethod($method); |
50
|
|
|
$this->setRequestHeaders($headers); |
51
|
|
|
$this->setPostBody($post_body); |
52
|
|
|
$this->setExpectedType($expected_type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $expectedType XML/JSON |
57
|
|
|
*/ |
58
|
|
|
public function setExpectedType($expectedType) |
59
|
|
|
{ |
60
|
|
|
$this->expectedType = strtoupper($expectedType); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param integer $httpResponseCode HTTP Response code |
65
|
|
|
*/ |
66
|
|
|
public function setHttpResponseCode($httpResponseCode) |
67
|
|
|
{ |
68
|
|
|
$this->httpResponseCode = $httpResponseCode; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $postBody array of post parameters |
73
|
|
|
*/ |
74
|
|
|
public function setPostBody($postBody) |
75
|
|
|
{ |
76
|
|
|
$this->postBody = $postBody; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $requestMethod HTTP Request method |
81
|
|
|
*/ |
82
|
|
|
public function setRequestMethod($requestMethod) |
83
|
|
|
{ |
84
|
|
|
$this->requestMethod = $requestMethod; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function getExpectedType() |
91
|
|
|
{ |
92
|
|
|
return $this->expectedType; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $responseBody |
97
|
|
|
*/ |
98
|
|
|
public function setResponseBody($responseBody) |
99
|
|
|
{ |
100
|
|
|
$this->responseBody = $responseBody; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $responseHeaders of HTTP response headers |
105
|
|
|
*/ |
106
|
|
|
public function setResponseHeaders($responseHeaders) |
107
|
|
|
{ |
108
|
|
|
$this->responseHeaders = $responseHeaders; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return integer HTTP response code |
113
|
|
|
*/ |
114
|
|
|
public function getHttpResponseCode() |
115
|
|
|
{ |
116
|
|
|
return $this->httpResponseCode; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $requestHeaders array with request headers with keys and values |
121
|
|
|
*/ |
122
|
|
|
public function setRequestHeaders($requestHeaders) |
123
|
|
|
{ |
124
|
|
|
$this->requestHeaders = $requestHeaders; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $url this should be a valid HTTP URL |
129
|
|
|
*/ |
130
|
|
|
public function setUrl($url) |
131
|
|
|
{ |
132
|
|
|
$this->url = $url; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed response body |
137
|
|
|
*/ |
138
|
|
|
public function getResponseBody() |
139
|
|
|
{ |
140
|
|
|
return $this->responseBody; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array HTTP Reponse headers |
145
|
|
|
*/ |
146
|
|
|
public function getResponseHeaders() |
147
|
|
|
{ |
148
|
|
|
return $this->responseHeaders; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string HTTP Request Method |
153
|
|
|
*/ |
154
|
|
|
public function getRequestMethod() |
155
|
|
|
{ |
156
|
|
|
return $this->requestMethod; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array $requestHeaders as the headers set for this request |
161
|
|
|
*/ |
162
|
|
|
public function getRequestHeaders() |
163
|
|
|
{ |
164
|
|
|
return $this->requestHeaders; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string url return the requested url |
169
|
|
|
*/ |
170
|
|
|
public function getUrl() |
171
|
|
|
{ |
172
|
|
|
return $this->url; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array of post body parameters |
177
|
|
|
*/ |
178
|
|
|
public function getPostBody() |
179
|
|
|
{ |
180
|
|
|
return $this->postBody; |
181
|
|
|
} |
182
|
|
|
} |