The sFire\HTTP\Request object is a handy tool for getting lots of information about the users request. Showing all the headers, retrieving POST information, determine if the request is a GET method and returning the users IP address are just some of the functionality you may use.
sFire lets you determine the request method with different methods for you to use. There are some fixed methods and a more flexible function.
The "isMethod" method accepts one parameter to determine the request method. The parameter is case insensitive.
Request :: isMethod(string $method);
if(true === Request :: isMethod('post')) {
//Method is POST
}
The "getMethod" returns a string in lowercase with the request method.
if('get' === Request :: getMethod()) {
//Method is GET
}
Check if the request method is GET.
if(true === Request :: isGet()) {
//Method is GET
}
Check if the request method is POST.
if(true === Request :: isPost()) {
//Method is POST
}
Check if the request method is DELETE.
if(true === Request :: isDelete()) {
//Method is DELETE
}
Check if the request method is PATCH.
if(true === Request :: isPatch()) {
//Method is PATCH
}
Check if the request method is CONNECT.
if(true === Request :: isConnect()) {
//Method is CONNECT
}
Check if the request method is HEAD.
if(true === Request :: isHead()) {
//Method is HEAD
}
Check if the request method is OPTIONS.
if(true === Request :: isOptions()) {
//Method is OPTIONS
}
Check if the request method is TRACE.
if(true === Request :: isTrace()) {
//Method is TRACE
}
You can enable or disable options for returning data with from* methods like the fromGet or fromPost method (discussed below).
| Option | Description |
|---|---|
| EMPTY_STRING_TO_NULL | When data is a String and is empty, return NULL or default value |
| EMPTY_ARRAY_TO_NULL | When data is an Array and is empty, return NULL or default value |
| TRIM_STRING | When data is a string, trim the data and return it |
To enable an option, call the enable method.
Request :: enable($option);
To disable an option, call the disable method.
Request :: disable($option);
$_POST['foo'] = ' bar ';
Request :: enable(Request :: TRIM_STRING);
Request :: fromPost('foo'); //Returns "bar" (without white space)
The example below will return "bar" because it will trim the data and then converts empty Strings to NULL values that will trigger the default value in the second parameter.
$_POST['foo'] = ' ';
Request :: enable(Request :: TRIM_STRING);
Request :: enable(Request :: EMPTY_STRING_TO_NULL);
Request :: fromPost('foo', 'bar'); //Returns "bar"
You can retrieve data from a specific method or just return all the data with the all method. This is based on a key/value structure. If you want to parse the request body yourself, you can call the getBody method.
Returns an Array with the data from all methods.
#data = Request :: all();
print_r($data);
The getBody method will return the request body so you can parse the body yourself.
$data = Request :: getBody();
print_r($data);
Returns an Array with all the GET variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP GET method
$data = Request :: fromGet();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromGet('key', 'default value');
Returns an Array with all the POST variables. You can also retrieve a value by giving a key as first parameter. The second and last parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP POST method
$data = Request :: fromPost();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromPost('key', 'default value');
Returns an Array with all the DELETE variables. You can also retrieve a value by giving a key as first parameter. The second and last parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP DELETE method
$data = Request :: fromDelete();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromDelete('key', 'default value');
Returns an Array with all the PATCH variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP PATCH method
$data = Request :: fromPatch();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromPatch('key', 'default value');
Returns an Array with all the CONNECT variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP CONNECT method
$data = Request :: fromConnect();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromConnect('key', 'default value');
Returns an Array with all the HEAD variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP HEAD method
$data = Request :: fromHead();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromHead('key', 'default value');
Returns an Array with all the OPTIONS variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP OPTIONS method
$data = Request :: fromOptions();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromOptions('key', 'default value');
Returns an Array with all the TRACE variables. You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from HTTP TRACE method
$data = Request :: fromTrace();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromTrace('key', 'default value');
Returns an Array with all the data from the current HTTP method. If the current HTTP method is a POST, than it will return the POST variables and so on. This way, you don't have to determine the current method to know it's source.
You can also retrieve a value by giving a key as first parameter. The second parameter is a default value that will be returned if the key could not be found.
//Retrieve all data from current HTTP method
$data = Request :: fromCurrent();
print_r($data);
//Or retrieve a single value based on a key. If the key is not set, "default value" will be returned
Request :: fromCurrent('key', 'default value');
When dealing with uploaded files, we suggest using the "getUploadedFiles" method for retrieving all the files as an logically Array.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image[1]">
<input type="file" name="image[2]">
<input type="submit">
</form>
In the Array below, you see the output of the two files submitted. This is the standard PHP approach which is not logically build.
Array
(
[file] => Array
(
[name] => Array
(
[1] => 20150814_141414.jpg
[2] => 20171006_194613.jpg
)
[type] => Array
(
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[1] => /tmp/phpXgP2ul
[2] => /tmp/phpVAbqh6
)
[error] => Array
(
[1] => 0
[2] => 0
)
[size] => Array
(
[1] => 1527594
[2] => 996876
)
)
)
Instead of using the $_FILES global variable, you can use the getUploadedFiles method. This method returns the same as $_FILES when the files are not named as an Array, but will produce a better Array when they are named as an Array.
$files = Request :: getUploadedFiles();
print_r($files);
The output will be:
Array
(
[file] => Array
(
[1] => Array
(
[name] => 20150814_141414.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpXgP2ul
[error] => 0
[size] => 1527594
)
[2] => Array
(
[name] => 20171006_194613.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpVAbqh6
[error] => 0
[size] => 996876
)
)
)
There are various methods for getting header information.
This method returns all the headers from the request.
$headers = Request :: getHeaders();
print_r($headers);
This method returns a single header from the request by giving the header name (key). Returns NULL if the header could not be found.
Request :: getHeader(string $header);
$header = Request :: getHeader('content-type');
echo $header;
This method returns a Boolean true if a header exists in the request headers or Boolean false if not.
Request :: hasHeader(string $header);
if(true === Request :: hasHeader('content-type') {
//Header exists
}
There is usually lots of valuable information in the request like the request IP address or UserAgent. sFire lets you return the information from the user with some easy to use tools.
Retrieve the user IP address. The user IP address is determined from the HTTP_X_FORWARDED_FOR, HTTP-X-FORWARDED-FOR, HTTP_VIA OR REMOTE_ADDR.
$ip = Request :: getIp();
echo $ip;
Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page.
$useragent = Request :: getUserAgent();
echo $useragent;
Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'.
$connection = Request :: getConnection();
echo $connection;
Contents of the Cache-Control: header from the current request, if there is one.
$cachecontrol = Request :: getCacheControl();
echo $cachecontrol;
Contents of the Accept: header from the current request, if there is one.
$accept = Request :: getAccept();
echo $accept;
Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.
$encoding = Request :: getAcceptedEncoding();
echo $encoding;
Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.
$language = Request :: getAcceptedLanguage();
echo $language;
When doing HTTP authentication this variable is set to the authentication type.
$authentication = Request :: getAuthentication();
echo $authentication;
Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';
$protocol = Request :: getProtocol();
echo $protocol;
When doing HTTP authentication this variable is set to the username provided by the user.
$user = Request :: getUser();
echo $user;
When doing HTTP authentication this variable is set to the password provided by the user.
$password = Request :: getPassword();
echo $password;
Contents of the Accept-Charset: header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'.
$characterset = Request :: getCharacterSet();
echo $characterset;
The timestamp of the start of the request, with microsecond precision.
$time = Request :: getRequestTime();
echo $time;
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user browser will set this, and some provide the ability to modify the referer as a feature. In short, it cannot really be trusted.
$referer = Request :: getReferer();
echo $referer;
Contents of the Content-Length: header from the current request, if there is one.
$length = Request :: getContentLength();
echo $length;
The URI which was given in order to access this page; for instance, '/customer/overview'.
$uri = Request :: getUri();
echo $uri;
Contents of the Host: header from the current request, if there is one.
$host = Request :: getHost();
echo $host;