Download the PHP package josantonius/request without Composer
On this page you can find all versions of the php package josantonius/request. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download josantonius/request
More information about josantonius/request
Files in josantonius/request
Informations about the package request
PHP Request library
Versión en español
PHP library for handling requests.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- Sponsor
- License
Requirements
This library is supported by PHP 7.4.
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.
Installation
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
Available methods in this library:
- Check if it's a GET request
# Return (boolean)
- Check if it's a POST request
# Return (boolean)
- Check if it's a PUT request
# Return (boolean)
- Check if it's a DELETE request
# Return (boolean)
- Get request params
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:
- application/atom+xml
- text/html
- text/plain
- application/json
- application/javascript
- multipart/form-data
- application/x-www-form-urlencoded
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$type | Request type. | string | Yes |
# Return anonymous function that will return the Request object when it's called
- Data sanitation and return as array
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
- Data sanitation and return as object
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
- Data sanitation and return as JSON
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
- Data sanitation and return as string
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
- Data sanitation and return as integer
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
- Data sanitation and return as float
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
- Data sanitation and return as boolean
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
- Data sanitation and return as IP address
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
- Data sanitation and return as URL
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
- Data sanitation and return as email
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
Quick Start
To use this library with Composer:
Or If you installed it manually, use it:
Usage
For the examples it will be simulated that the following data is received in the request:
Example data received in the request
Example of use for this library:
- Check if it's a GET request
- Check if it's a POST request
- Check if it's a PUT request
- Check if it's a DELETE request
- Access to the parameters of the request
Returns an anonymous function that will return the Request object when it's called.
- As array
- Get and sanitize all data and return them as array
- Example data received in the request
- Obtain, sanitize all data and return them as array and filter each value according to the data type
- Example data received in the request
- 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
- Example data received in the request
- As object
- Get and sanitize all data and return them as object
- Example data received in the request
- Obtain, sanitize all data and return them as object and filter each value according to the data type
- Example data received in the request
- 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
- Example data received in the request
- As JSON
- Get and sanitize all data and return them as JSON
- Example data received in the request
- Get specific key value, sanitize data and return them as JSON
- Example data received in the request
- As string
- Get specific key value, sanitize data and return them as string
- Example data received in the request
- As integer
- Get specific key value, sanitize data and return them as integer
- Example data received in the request
- As float
- Get specific key value, sanitize data and return them as float
- Example data received in the request
- As boolean
- Get specific key value, sanitize data and return them as boolean
- As IP
- Get specific key value, sanitize data and return them as IP
- Example data received in the request
- As URL
filterRequest
- Get specific key value, sanitize data and return them as URL
- Example data received in the request
- As email
- Get specific key value, sanitize data and return them as email
- Example data received in the request
Tests
To run 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
Sponsor
If this project helps you to reduce your development time, you can sponsor me to support my open source work :blush:
License
This repository is licensed under the MIT License.
Copyright © 2017-2022, Josantonius