bayfrontmedia/php-http-request

Easily handle data from incoming HTTP requests.

v3.1.0 2024-09-10 01:13 UTC

This package is auto-updated.

Last update: 2024-11-10 01:31:56 UTC


README

Easily handle data from incoming HTTP requests.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0

Installation

composer require bayfrontmedia/php-http-request

Usage

Request methods

Data types

Specific values

validateMethod

Description:

Returns valid request method with a fallback to GET.

Valid request methods include:

  • CONNECT
  • DELETE
  • GET
  • HEAD
  • OPTIONS
  • PATCH
  • POST
  • PUT
  • TRACE

Parameters:

  • $method (string)

Returns:

  • (string)

Example:

use Bayfront\HttpRequest\Request;

echo Request::validateMethod('GET');

getMethod

Description:

Returns current request method.

Parameters:

  • None

Returns:

  • (string)

Example:

use Bayfront\HttpRequest\Request;

echo Request::getMethod();

isConnect

Description:

Is current request method CONNECT.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isConnect()) {
    // Do something
}

isDelete

Description:

Is current request method DELETE.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isDelete()) {
    // Do something
}

isGet

Description:

Is current request method GET.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isGet()) {
    // Do something
}

isHead

Description:

Is current request method HEAD.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isHead()) {
    // Do something
}

isOptions

Description:

Is current request method OPTIONS.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isOptions()) {
    // Do something
}

isPatch

Description:

Is current request method PATCH.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isPatch()) {
    // Do something
}

isPost

Description:

Is current request method POST.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isPost()) {
    // Do something
}

isPut

Description:

Is current request method PUT.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isPut()) {
    // Do something
}

isTrace

Description:

Is current request method TRACE.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isTrace()) {
    // Do something
}

getFile

Description:

Returns value of single $_FILES array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getFile('profile_photo'));

hasFile

Description:

Checks if $_FILES array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasFile('profile_photo')) {
    // Do something
}

getQuery

Description:

Returns value of single $_GET array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getQuery());

hasQuery

Description:

Checks if $_GET array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasQuery('page')) {
    // Do something
}

getPost

Description:

Returns value of single $_POST array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getPost());

hasPost

Description:

Checks if $_POST array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasPost('username')) {
    // Do something
}

getServer

Description:

Returns value of single $_SERVER array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getServer());

hasServer

Description:

Checks if $_SERVER array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasServer('SERVER_NAME')) {
    // Do something
}

getCookie

Description:

Returns value of single $_COOKIE array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getCookie());

hasCookie

Description:

Checks if $_COOKIE array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasCookie('cart_id')) {
    // Do something
}

getHeader

Description:

Returns value of single header array key in dot notation or entire array, with optional default value.

Parameters:

  • $key = NULL (string|null)
  • $default = NULL (mixed): Default value to return if array key is not found

Returns:

  • (mixed)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getHeader());

hasHeader

Description:

Checks if header array key exists in dot notation.

Parameters:

  • $key (string)

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasHeader('Content-Type')) {
    // Do something
}

getBody

Description:

Returns content body of a request.

Parameters:

  • None

Returns:

  • (string)

Example:

use Bayfront\HttpRequest\Request;

print_r(Request::getBody());

hasBody

Description:

Checks if content body of a request exists.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::hasBody()) {
    // Do something
}

getUserAgent

Description:

Returns client's user agent.

Parameters:

  • None

Returns:

  • (mixed): string|null

Example:

use Bayfront\HttpRequest\Request;

echo Request::getUserAgent();

getReferer

Description:

Returns client's referring URL.

Parameters:

  • None

Returns:

  • (mixed): string|null

Example:

use Bayfront\HttpRequest\Request;

echo Request::getReferer();

getIp

Description:

Returns the most probable IP of client with optional default value.

Parameters:

  • $default = '' (string): Default IP address to return if none detected

Returns:

  • (string)

Example:

use Bayfront\HttpRequest\Request;

echo Request::getIp();

isCli

Description:

Is the request originating from the command line.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isCli()) {
    // Do something
}

isJson

Description:

Is the Content-Type header for this request JSON.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isJson()) {
    // Do something
}

wantsJson

Description:

Does the Accept header for this request expect JSON.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::wantsJson()) {
    // Do something
}

isHttps

Description:

Is connection HTTPS.

Parameters:

  • None

Returns:

  • (bool)

Example:

use Bayfront\HttpRequest\Request;

if (Request::isHttps()) {
    // Do something
}

getRequest

Description:

Returns array containing details of the client's request, or string of a specific part of the request.

Parameters:

  • $part = '' (string): Which part of the request to return. Leaving this blank will return the entire array.

Valid $part values include any of the PART_* constants:

  • Requst::PART_METHOD
  • Request::PART_PROTOCOL
  • Request::PART_HOST
  • Request::PART_PATH
  • Request::PART_QUERY
  • Request::PART_QUERY_STRING
  • Request::PART_URL
  • Request::PART_FULL_URL

Returns:

  • (mixed): array|string

Example:

use Bayfront\HttpRequest\Request;

echo Request::getRequest(Request::PART_URL);

getUrl

Description:

Returns current URL.

Parameters:

  • $include_query = false (bool): Include the query string, if existing

Returns:

  • (string)

Example:

use Bayfront\HttpRequest\Request;

echo Request::getUrl();