sFire PHP Framework

Request

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.

In this section we will handle:

  • Knowing which request method is used like POST or GET etc.
  • Enable or disable options for retrieving values
  • Retrieving GET, POST, DELETE, PATCH, CONNECT, HEAD, OPTIONS and TRACE data
  • Retrieving uploaded files
  • Retrieving header information
  • Retrieving user information like IP address

Knowing which request method is used

sFire lets you determine the request method with different methods for you to use. There are some fixed methods and a more flexible function.

isMethod

The "isMethod" method accepts one parameter to determine the request method. The parameter is case insensitive.

Parameters
Request :: isMethod(string $method);
Example
if(true === Request :: isMethod('post')) {
    //Method is POST
}

getMethod

The "getMethod" returns a string in lowercase with the request method.

Example
if('get' === Request :: getMethod()) {
    //Method is GET
}

isGet

Check if the request method is GET.

if(true === Request :: isGet()) {
    //Method is GET
}

isPost

Check if the request method is POST.

if(true === Request :: isPost()) {
    //Method is POST
}

isDelete

Check if the request method is DELETE.

if(true === Request :: isDelete()) {
    //Method is DELETE
}

isPatch

Check if the request method is PATCH.

if(true === Request :: isPatch()) {
    //Method is PATCH
}

isConnect

Check if the request method is CONNECT.

if(true === Request :: isConnect()) {
    //Method is CONNECT
}

isHead

Check if the request method is HEAD.

if(true === Request :: isHead()) {
    //Method is HEAD
}

isOptions

Check if the request method is OPTIONS.

if(true === Request :: isOptions()) {
    //Method is OPTIONS
}

isTrace

Check if the request method is TRACE.

if(true === Request :: isTrace()) {
    //Method is TRACE
}

Enable or disable options for retrieving values

You can enable or disable options for returning data with from* methods like the fromGet or fromPost method (discussed below).

Available options
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
Enable options

To enable an option, call the enable method.

Request :: enable($option);
Disable options

To disable an option, call the disable method.

Request :: disable($option);
Example 1
$_POST['foo'] = '  bar     ';

Request :: enable(Request :: TRIM_STRING);
Request :: fromPost('foo'); //Returns "bar" (without white space)
Example 2

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"

Retrieving GET, POST, DELETE, PATCH, CONNECT, HEAD, OPTIONS and TRACE data

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.

All

Returns an Array with the data from all methods.

#data = Request :: all();
print_r($data);

getBody

The getBody method will return the request body so you can parse the body yourself.

$data = Request :: getBody();
print_r($data);

fromGet

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');

fromPost

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');

fromDelete

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');

fromPatch

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');

fromConnect

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');

fromHead

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');

fromOptions

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');

fromTrace

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');

fromCurrent

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');

Retrieving uploaded files

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>
$_FILES example

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
                )
        )
)
Get Uploaded Files

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
                )
        )
)

Retrieving header information

There are various methods for getting header information.

getHeaders

This method returns all the headers from the request.

$headers = Request :: getHeaders();
print_r($headers);

getHeader

This method returns a single header from the request by giving the header name (key). Returns NULL if the header could not be found.

Parameters
Request :: getHeader(string $header);
Example
$header = Request :: getHeader('content-type');
echo $header;

hasHeader

This method returns a Boolean true if a header exists in the request headers or Boolean false if not.

Parameters
Request :: hasHeader(string $header);
Example
if(true === Request :: hasHeader('content-type') {
    //Header exists
}

Retrieving user information

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.

IP address

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;

User agent

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;

Connection

Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'.

$connection = Request :: getConnection();
echo $connection;

Cache control

Contents of the Cache-Control: header from the current request, if there is one.

$cachecontrol = Request :: getCacheControl();
echo $cachecontrol;

Accept

Contents of the Accept: header from the current request, if there is one.

$accept = Request :: getAccept();
echo $accept;

Accepted encoding

Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.

$encoding = Request :: getAcceptedEncoding();
echo $encoding;

Accepted language

Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.

$language = Request :: getAcceptedLanguage();
echo $language;

Authentication

When doing HTTP authentication this variable is set to the authentication type.

$authentication = Request :: getAuthentication();
echo $authentication;

Protocol

Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';

$protocol = Request :: getProtocol();
echo $protocol;

User

When doing HTTP authentication this variable is set to the username provided by the user.

$user = Request :: getUser();
echo $user;

Password

When doing HTTP authentication this variable is set to the password provided by the user.

$password = Request :: getPassword();
echo $password;

Character set

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;

Request time

The timestamp of the start of the request, with microsecond precision.

$time = Request :: getRequestTime();
echo $time;

Referer

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;

Content length

Contents of the Content-Length: header from the current request, if there is one.

$length = Request :: getContentLength();
echo $length;

Uri

The URI which was given in order to access this page; for instance, '/customer/overview'.

$uri = Request :: getUri();
echo $uri;

Host

Contents of the Host: header from the current request, if there is one.

$host = Request :: getHost();
echo $host;