2017 © Pedro Peláez
 

library request

PHP library for handling requests.

image

josantonius/request

PHP library for handling requests.

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 100 % Grown

The README.md

PHP Request library

Latest Stable Version License, (*1)

Versión en español, (*2)

PHP library for handling requests., (*3)



Requirements

This library is supported by PHP 7.4., (*4)

IMPORTANT: Version 2.x does not support version 1.x of this library., (*5)

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., (*6)

Installation

The preferred way to install this extension is through Composer., (*7)

To install PHP Request library, simply:, (*8)

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:, (*9)

composer require josantonius/request --prefer-source

You can also clone the complete repository with Git:, (*10)

$ git clone https://github.com/Josantonius/php-request.git, (*11)

Or install it manually:, (*12)

Download Request.php and Validate.php:, (*13)

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

Available methods in this library:, (*14)

- Check if it's a GET request

Request::isGet();

# Return (boolean), (*15)

- Check if it's a POST request

Request::isPost();

# Return (boolean), (*16)

- Check if it's a PUT request

Request::isPut();

# Return (boolean), (*17)

- Check if it's a DELETE request

Request::isDelete();

# Return (boolean), (*18)

- Get request params

For PUT and DELETE requests, the content type will be checked to correctly obtain the data received in the request., (*19)

The content types compatible with this library are:, (*20)

  • application/atom+xml
  • text/html
  • text/plain
  • application/json
  • application/javascript
  • multipart/form-data
  • application/x-www-form-urlencoded
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, (*21)

- Data sanitation and return as array

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, (*22)

- Data sanitation and return as object

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, (*23)

- Data sanitation and return as JSON

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, (*24)

- Data sanitation and return as string

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, (*25)

- Data sanitation and return as integer

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, (*26)

- Data sanitation and return as float

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, (*27)

- Data sanitation and return as boolean

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, (*28)

- Data sanitation and return as IP address

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, (*29)

- Data sanitation and return as URL

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, (*30)

- Data sanitation and return as email

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, (*31)

Quick Start

To use this library with Composer:, (*32)

require __DIR__ . '/vendor/autoload.php';

use Josantonius\Request\Request;

Or If you installed it manually, use it:, (*33)

require_once __DIR__ . '/Request.php';
require_once __DIR__ . '/Validate.php';

use Josantonius\Request\Request;
use Josantonius\Validate\Validate;

Usage

For the examples it will be simulated that the following data is received in the request:, (*34)

Example data 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' => 'john@site.com'
'user_address' => [
  'street' => 'unknown'
  'locality' => 'Seville'
  'country' => 'Spain'
]
'is_active' => true

Example of use for this library:, (*35)

- Check if it's a GET request

Request::isGet(); // true or false

- Check if it's a POST request

Request::isPost(); // true or false

- Check if it's a PUT request

Request::isPut(); // true or false

- Check if it's a DELETE request

Request::isDelete(); // true or false

- Access to the parameters of the request

$_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., (*36)

- As array

- Get and sanitize all data and return them as array

$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) "john@site.com"
  ["user_address"]=>
  array(3) {
    ["street"]=>
    string(7) "unknown"
    ["locality"]=>
    string(7) "Seville"
    ["country"]=>
    string(5) "Spain"
  }
  ["is_active"]=>
  bool(true)
}
*/

- Obtain, sanitize all data and return them as array and filter each value according to the data type

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

- Obtain, sanitize all data and return them as array, filter each value according to the data type and specify a value for each key when it's wrong

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

- As object

- Get and sanitize all data and return them as object

$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) "john@site.com"
  ["user_address"]=>
  object(stdClass)#2 (3) {
    ["street"]=>
    string(7) "unknown"
    ["locality"]=>
    string(7) "Seville"
    ["country"]=>
    string(5) "Spain"
  }
  ["is_active"]=>
  bool(true)
}
*/

- Obtain, sanitize all data and return them as object and filter each value according to the data type

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

- Obtain, sanitize all data and return them as object, filter each value according to the data type and specify a value for each key when it's wrong

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

- As JSON

- Get and sanitize all data and return them as JSON

$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":"john@site.com","user_address":{"street":"unknown","locality":"Seville","country":"Spain"},"is_active":true}"
*/

- Get specific key value, sanitize data and return them as JSON

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

- As string

- Get specific key value, sanitize data and return them as string

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

- As integer

- Get specific key value, sanitize data and return them as integer

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

- As float

- Get specific key value, sanitize data and return them as float

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

- As boolean

- Get specific key value, sanitize data and return them as boolean

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

- As IP

- Get specific key value, sanitize data and return them as IP

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

- As URL

filterRequest, (*37)

- Get specific key value, sanitize data and return them as URL

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

- As email

- Get specific key value, sanitize data and return them as email

$email = $_GET('user_website')->asEmail();

var_dump($email); // string(13) "john@site.com"
$email = $_POST('user_rating')->asEmail();

var_dump($email); // NULL (it's not an email, the default value is returned)
$email = $_DELETE('user_name')->asEmail("john@site.com");

var_dump($email); // string(13) "john@site.com" (it's not an email, the default value is returned)

Tests

To run tests you just need composer and to execute the following:, (*38)

git clone https://github.com/Josantonius/php-request.git

cd php-request

composer install

Run unit tests with PHPUnit:, (*39)

gnome-terminal -e 'php -S localhost:8000 -t tests/'

composer phpunit

Run PSR2 code standard tests with PHPCS:, (*40)

composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:, (*41)

composer phpmd

Run all previous tests:, (*42)

composer tests

If this project helps you to reduce your development time, you can sponsor me to support my open source work :blush:, (*43)

License

This repository is licensed under the MIT License., (*44)

Copyright © 2017-2022, Josantonius, (*45)

The Versions

07/01 2018

dev-master

9999999-dev

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

07/01 2018

1.1.7

1.1.7.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

13/11 2017

1.1.6

1.1.6.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

09/11 2017

1.1.5

1.1.5.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

02/11 2017

1.1.4

1.1.4.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

14/09 2017

1.1.3

1.1.3.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php ajax request delete put post get http methods

18/07 2017

1.1.2

1.1.2.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

php ajax request delete put post get http methods

18/03 2017

1.1.1

1.1.1.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

php ajax request delete put post get http methods

17/01 2017

1.0.0

1.0.0.0

PHP library for handling requests.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

php ajax request delete put http methods get post