3 namespace Services\Http
;
5 use Services\Http\IHttpRequest
;
6 use Services\Http\Util
;
8 class HttpRequest
implements IHttpRequest
10 const METHOD_GET
= 'GET';
11 const METHOD_POST
= 'POST';
12 const METHOD_PUT
= 'PUT';
13 const METHOD_DELETE
= 'DELETE';
14 const METHOD_HEAD
= 'HEAD';
18 protected $_formDataMediaTypes = array('application/x-www-form-urlencoded');
20 public function __construct()
22 $this->_method
= $_SERVER['REQUEST_METHOD'];
24 if(isset($_SERVER['HTTP_COOKIE'])) {
25 $this->_cookies
= Util
::parseCookieHeader($_SERVER['HTTP_COOKIE']);
29 public function getMethod()
31 return $this->_method
;
34 public function isGet()
36 return $this->_method
== self
::METHOD_GET
;
39 public function isPost()
41 return $this->_method
== self
::METHOD_POST
;
44 public function isPut()
46 return $this->_method
== self
::METHOD_PUT
;
49 public function isHead()
51 return $this->_method
== self
::METHOD_HEAD
;
54 public function isDelete()
56 return $this->_method
== self
::METHOD_DELETE
;
59 public function getContentType()
61 return isset($_SERVER['CONTENT_TYPE']) ?
$_SERVER['CONTENT_TYPE'] : null
;
64 public function isFormData()
66 return in_array($this->getContentType(), $this->_formDataMediaTypes
);
71 return !empty($_GET) ?
$_GET : null
;
74 public function getGetElement($key)
76 return isset($_GET[$key]) ?
$_GET[$key] : null
;
79 public function post()
81 return !empty($_POST) ?
$_POST : null
;
84 public function getPostElement($key)
86 return isset($_POST[$key]) ?
$_POST[$key] : null
;
94 public function getPutElement($key)
96 return $this->getPostElement($key);
99 public function delete()
101 return $this->post();
104 public function getDeleteElement($key)
106 return $this->getPostElement($key);
109 public function cookies()
111 return $this->_cookies
;
114 public function getCookie($key)
116 return isset($this->_cookies
[$key]) ?
$this->_cookies
[$key] : null
;
119 public function getBody()
121 return @file_get_contents
('php://input');
123 public function getHost()
125 if(isset($_SERVER['HTTP_HOST'])) {
126 $parts = explode(':', $_SERVER['HTTP_HOST']);
130 return $_SERVER['SERVER_NAME'];
133 public function getIp()
135 $keys = array('X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP', 'REMOTE_ADDR');
136 foreach ($keys as $key) {
137 if(isset($_SERVER[$key])) {
138 return $_SERVER[$key];
143 public function getReferrer()
145 return $_SERVER['HTTP_REFERER'];
148 // some people like to spell it referer due to RFC 1945
149 public function getReferer()
151 return $this->getReferer();
154 public function getUserAgent()
156 return $_SERVER['HTTP_USER_AGENT'];
159 private function getPhysicalPath()
161 $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php"
162 $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
165 if (strpos($requestUri, $scriptName) !== false
) {
166 $physicalPath = $scriptName; // <-- Without rewriting
168 $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting
171 return $physicalPath;
174 public function getPath()
176 $physicalPath = $this->getPhysicalPath();
177 $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
178 $queryString = isset($_SERVER['QUERY_STRING']) ?
$_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or ""
181 $path = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
182 $path = str_replace('?' . $queryString, '', $path); // <-- Remove query string
183 $path = '/' . ltrim($path, '/'); // <-- Ensure leading slash
184 $path = rtrim($path, '/'); // remove / from the end of string