-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
83 lines (70 loc) · 1.94 KB
/
Request.php
File metadata and controls
83 lines (70 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace NGFramer\NGFramerPHPBase;
class Request
{
/**
* Returns the path the user is trying to access from the URL.
*
* @return string
*/
public function getPath(): string
{
$uri = $_SERVER['REQUEST_URI'] ?? '/';
return parse_url($uri)['path'];
}
/**
* Returns the method the user is trying to access the URL.
*
* @return string
*/
public function getMethod(): string
{
return strtolower($_SERVER['REQUEST_METHOD']);
}
/**
* Returns the header data sent in the request.
*
* @return array
*/
public function getHeader(): array
{
// Get the headers from the apache_request_headers function.
$result = apache_request_headers();
// Check the result and return the headers.
if (!$result) return [];
return $result;
}
/**
* Returns the data sent in the request.
*
* @return array
*/
public function getBody(): array
{
// Initialize an array to save data and get method of sending data.
$method = $this->getMethod();
$data = [];
// Check for the request method of sending data and fetch them.
if ($method === 'get') {
foreach ($_GET as $key => $value) {
$data[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
} elseif ($method === 'post') {
foreach ($_POST as $key => $value) {
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
// Return the sanitized data.
return $data;
}
/**
* Returns the raw body data sent in the request.
*
* @return array
*/
public function getRawBody(): array
{
// Get the raw body data from the php://input stream.
return (array)json_decode(file_get_contents("php://input"), true);
}
}