| Recommend this page to a friend! | 
|  Download | 
| Info | Documentation |  Files |  Install with Composer |  Download | Reputation | Support forum | Blog | Links | 
| Ratings | Unique User Downloads | Download Rankings | ||||
| Not yet rated by the users | Total: 161 | All time:  8,956 This week: 673  | ||||
| Version | License | PHP version | Categories | |||
| php-request 1.1.5 | MIT/X Consortium ... | 5.6 | HTTP, PHP 5 | 
| Description | Author | |
| This class can check the HTTP request and extract its parameters. | 
PHP library for handling requests.
This library is supported by PHP versions 7.0 or higher.
IMPORTANT: Version 2.x does not support version 1.x of this library.
IMPORTANT: Version 1.x has been considered obsolete, but if you want to use it for 5.6 versions of PHP you can have a look at its documentation.
The preferred way to install this extension is through Composer.
To install PHP Request library, simply:
$ composer require josantonius/request
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
$ composer require josantonius/request --prefer-source
You can also clone the complete repository with Git:
$ git clone https://github.com/Josantonius/php-request.git
Or install it manually:
Download Request.php and Validate.php:
$ wget https://raw.githubusercontent.com/Josantonius/php-request/master/src/Request.php
$ wget https://raw.githubusercontent.com/Josantonius/php-validate/master/src/Validate.php
Available methods in this library:
Request::isGet();
# Return (boolean)
Request::isPost();
# Return (boolean)
Request::isPut();
# Return (boolean)
Request::isDelete();
# Return (boolean)
For PUT and DELETE requests, the content type will be checked to correctly obtain the data received in the request.
The content types compatible with this library are:
Request::input($type);
| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $type | Request type. | string | Yes | |
# Return anonymous function that will return the Request object when it's called
asArray($filters, $default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $filters | Associative array with data type for each key. Fields that are not included in the filters will not be sanitized. | array | No | [] |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated.  | mixed | No | null |
# Return (array) ? it will return an empty array in case of error
asObject($filters, $default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $filters | Associative array with data type for each key. Fields that are not included in the filters will not be sanitized. | array | No | [] |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (object) ? it will return an empty object in case of error
asJson($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asString($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asInteger($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asFloat($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asBoolean($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asIp($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asUrl($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
asEmail($default);
| Attribute | Description | Type | Required | Default
| --- | --- | --- | --- | --- |
| $default | Default value. Null or the default value will be returned for fields that do not match with the data type indicated. | mixed | No | null |
# Return (mixed|null) ? value, null or customized return value
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php';
use Josantonius\Request\Request;
Or If you installed it manually, use it:
require_once __DIR__ . '/Request.php';
require_once __DIR__ . '/Validate.php';
use Josantonius\Request\Request;
use Josantonius\Validate\Validate;
For the examples it will be simulated that the following data is received in the request:
'user_name' => 'John'
'user_surname' => 'Doe'
'user_age' => 35
'user_rating' => 8.5
'user_ip' => '89.58.54.188'
'user_website' => 'http://www.site.com/'
'user_email' => '[email protected]'
'user_address' => [
  'street' => 'unknown'
  'locality' => 'Seville'
  'country' => 'Spain'
]
'is_active' => true
Example of use for this library:
Request::isGet(); // true or false
Request::isPost(); // true or false
Request::isPut(); // true or false
Request::isDelete(); // true or false
$_GET = Request::input('GET');
$_POST = Request::input('POST');
$_PUT = Request::input('PUT');
$_DELETE = Request::input('DELETE');
Returns an anonymous function that will return the Request object when it's called.
$array = $_GET()->asArray();
$array = $_POST()->asArray();
$array = $_PUT()->asArray();
$array = $_DELETE()->asArray();
var_dump($array);
/*
array(9) {
  ["user_name"]=>
  string(4) "John"
  ["user_surname"]=>
  string(3) "Doe"
  ["user_age"]=>
  int(35)
  ["user_rating"]=>
  float(8.5)
  ["user_ip"]=>
  string(12) "89.58.54.188"
  ["user_website"]=>
  string(20) "http://www.site.com/"
  ["user_email"]=>
  string(13) "[email protected]"
  ["user_address"]=>
  array(3) {
    ["street"]=>
    string(7) "unknown"
    ["locality"]=>
    string(7) "Seville"
    ["country"]=>
    string(5) "Spain"
  }
  ["is_active"]=>
  bool(true)
}
*/
$filters = [
    'user_name' => 'string',
    'user_age' => 'string',
    'is_online' => 'boolean'
];
$array = $_GET()->asArray($filters);
$array = $_POST()->asArray($filters);
$array = $_PUT()->asArray($filters);
$array = $_DELETE()->asArray($filters);
var_dump($array['user_name']); // string(4) "John"
var_dump($array['user_age']); // string(2) "35" (although an integer is received, it's returned as a string)
var_dump($array['user_age']); // NULL (doesn't exist, the default value is returned)
$filters = [
    'user_rating' => 'float',
    'is_active' => 'boolean',
    'is_online' => 'boolean'
];
$array = $_GET()->asArray($filters, '');
$array = $_POST()->asArray($filters, '');
$array = $_PUT()->asArray($filters, '');
$array = $_DELETE()->asArray($filters, '');
var_dump($array['user_rating']); // float(8.5)
var_dump($array['is_active']); // bool(true)
var_dump($array['is_online']); // string(0) "" (doesn't exist, the default value is returned)
$object = $_GET()->asObject();
$object = $_POST()->asObject();
$object = $_PUT()->asObject();
$object = $_DELETE()->asObject();
var_dump($object);
/*
object(stdClass)#1 (9) {
  ["user_name"]=>
  string(4) "John"
  ["user_surname"]=>
  string(3) "Doe"
  ["user_age"]=>
  int(35)
  ["user_rating"]=>
  float(8.5)
  ["user_ip"]=>
  string(12) "89.58.54.188"
  ["user_website"]=>
  string(20) "http://www.site.com/"
  ["user_email"]=>
  string(13) "[email protected]"
  ["user_address"]=>
  object(stdClass)#2 (3) {
    ["street"]=>
    string(7) "unknown"
    ["locality"]=>
    string(7) "Seville"
    ["country"]=>
    string(5) "Spain"
  }
  ["is_active"]=>
  bool(true)
}
*/
$filters = [
    'user_name' => 'string',
    'user_age' => 'integer',
    'is_online' => 'boolean'
];
$object = $_GET()->asObject($filters);
$object = $_POST()->asObject($filters);
$object = $_PUT()->asObject($filters);
$object = $_DELETE()->asObject($filters);
var_dump($object->user_name); // string(4) "John"
var_dump($object->user_age); // int(35)
var_dump($object->user_age); // NULL (doesn't exist, the default value is returned)
$filters = [
    'user_rating' => 'float',
    'user_surname' => 'boolean',
    'is_online' => 'boolean',
    'is_member' => 'boolean'
];
$object = $_GET()->asObject($filters, false);
$object = $_POST()->asObject($filters, false);
$object = $_PUT()->asObject($filters, false);
$object = $_DELETE()->asObject($filters, false);
var_dump($object->user_rating); // float(8.5)
var_dump($object->user_surname); // string(3) "Doe"
var_dump($object->is_online); // bool(false) (doesn't exist, the default value is returned)
var_dump($object->is_member); // bool(false) (doesn't exist, the default value is returned)
$json = $_GET()->asJson();
$json = $_POST()->asJson();
$json = $_PUT()->asJson();
$json = $_DELETE()->asJson();
var_dump($json);
/*
string(260) "{"user_name":"John","user_surname":"Doe","user_age":35,"user_rating":8.5,"user_ip":"89.58.54.188","user_website":"http:\/\/www.site.com\/","user_email":"[email protected]","user_address":{"street":"unknown","locality":"Seville","country":"Spain"},"is_active":true}"
*/
$json = $_GET('user_address')->asJson();
var_dump($json); // string(59) "{"street":"unknown","locality":"Seville","country":"Spain"}"
$json = $_POST('user_name')->asJson();
var_dump($json); // string(6) ""John""
$json = $_PUT('is_online')->asJson();
var_dump($json); // NULL (doesn't exist, the default value is returned)
$json = $_DELETE('user_address')->asJson([]);
var_dump($json); // string(2) "[]" (doesn't exist, the default value is returned)
$string = $_GET('user_age')->asString();
var_dump($string); // string(2) "35" (although an integer is received, it's returned as a string)
$string = $_POST('user_name')->asString();
var_dump($string); // string(4) "John"
$string = $_PUT('user_address')->asString();
var_dump($string); // NULL (it's an array, the default value is returned)
$string = $_DELETE('user_address')->asString('unknown');
var_dump($string); // string(7) "unknown" (it's an array, the default value is returned)
$integer = $_GET('user_age')->asInteger();
var_dump($integer); // int(35)
$integer = $_PUT('user_rating')->asInteger();
var_dump($integer); // NULL (it's a float, the default value is returned)
$integer = $_DELETE('user_rating')->asInteger(5);
var_dump($integer); // int(5) (it's a float, the default value is returned)
$float = $_GET('user_age')->asFloat();
var_dump($float); // float(35) (although an integer is received, it's returned as a float)
$float = $_POST('user_rating')->asFloat();
var_dump($float); // float(8.5)
$float = $_PUT('user_name')->asFloat();
var_dump($float); // NULL (it's a string, the default value is returned)
$float = $_DELETE('user_name')->asFloat(5.5);
var_dump($float); // float(5.5) (it's a string, the default value is returned)
$_GET['is_active'] = true;
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_GET['is_active'] = 'true';
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_POST['is_active'] = '1';
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_POST['is_active'] = 1;
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_GET['is_active'] = false;
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_GET['is_active'] = 'false';
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_POST['is_active'] = '0';
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_POST['is_active'] = 0;
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$boolean = $_PUT('user_name')->asBoolean();
var_dump($boolean); // NULL (it's a string, the default value is returned)
$boolean = $_DELETE('is_online')->asBoolean(false);
var_dump($boolean); // bool(false) (doesn't exist, the default value is returned)
$ip = $_GET('user_ip')->asIp();
var_dump($ip); // string(12) "89.58.54.188"
$ip = $_POST('user_rating')->asIp();
var_dump($ip); // NULL (it's not an IP, the default value is returned)
$ip = $_DELETE('user_name')->asIp("87.32.48.164");
var_dump($ip); // string(12) "87.32.48.164" (it's not an IP, the default value is returned)
$url = $_GET('user_website')->asUrl();
var_dump($url); // string(20) "http://www.site.com/"
$url = $_POST('user_rating')->asUrl();
var_dump($url); // NULL (it's not an URL, the default value is returned)
$url = $_DELETE('user_name')->asUrl("http://www.site.com/");
var_dump($url); // string(20) "http://www.site.com/" (it's not an URL, the default value is returned)
$email = $_GET('user_website')->asEmail();
var_dump($email); // string(13) "[email protected]"
$email = $_POST('user_rating')->asEmail();
var_dump($email); // NULL (it's not an email, the default value is returned)
$email = $_DELETE('user_name')->asEmail("[email protected]");
var_dump($email); // string(13) "[email protected]" (it's not an email, the default value is returned)
To run tests you just need composer and to execute the following:
$ git clone https://github.com/Josantonius/php-request.git
$ cd php-request
$ composer install
Run unit tests with PHPUnit:
$ gnome-terminal -e 'php -S localhost:8000 -t tests/'
$ composer phpunit
Run PSR2 code standard tests with PHPCS:
$ composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
$ composer phpmd
Run all previous tests:
$ composer tests
If you would like to help, please take a look at the list of issues or the To Do checklist.
Pull requests
Thank you to all the people who already contributed to this project!
<img alt="peter279k" src="https://avatars2.githubusercontent.com/u/9021747?v=4&s=117" height="117" width="117">|<img alt="Mahdrentys" src="https://avatars2.githubusercontent.com/u/40216477?v=4&s=117" height="117" width="117">| :---:|:---:| peter279k| Mahdrentys|
The file structure from this repository was created with PHP-Skeleton.
This project is licensed under MIT license. See the LICENSE file for more info.
2017 - 2018 Josantonius, josantonius.com
If you find it useful, let me know :wink:
|  Files (22) | 
| File | Role | Description | ||
|---|---|---|---|---|
|  src (1 file) | ||||
|  tests (9 files) | ||||
|    .editorconfig | Data | Auxiliary data | ||
|    .php_cs.dist | Example | Example script | ||
|    .travis.yml | Data | Auxiliary data | ||
|    CHANGELOG.md | Data | Auxiliary data | ||
|    composer.json | Data | Auxiliary data | ||
|    CONDUCT.md | Data | Auxiliary data | ||
|    LICENSE | Lic. | License text | ||
|    phpcs.xml | Data | Auxiliary data | ||
|    phpmd.xml | Data | Auxiliary data | ||
|    phpunit.xml | Data | Auxiliary data | ||
|    README-ES.md | Doc. | Documentation | ||
|    README.md | Doc. | Documentation | ||
|  Files (22) | / | tests | 
| File | Role | Description | 
|---|---|---|
|  DeleteRequestProxy.php | Class | Class source | 
|  DeleteRequestTest.php | Class | Class source | 
|  GetRequestProxy.php | Class | Class source | 
|  GetRequestTest.php | Class | Class source | 
|  PostRequestProxy.php | Class | Class source | 
|  PostRequestTest.php | Class | Class source | 
|  PutRequestProxy.php | Class | Class source | 
|  PutRequestTest.php | Class | Class source | 
|  RequestTest.php | Class | Class source | 
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. | 
|  Install with Composer | 
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% | 
 | 
 | 
| Applications that use this package | 
 If you know an application of this package, send a message to the author to add a link here.
 If you know an application of this package, send a message to the author to add a link here.