ramapriya/http-request

small library for work with http requests

dev-master 2020-05-01 02:20 UTC

This package is auto-updated.

Last update: 2025-05-29 01:08:12 UTC


README

Small PHP-SDK library for work with HTTP requests

Installation

Add dependency in composer.json

"require": {
    "ramapriya/http-request": "dev-master"
}

Evoke autoload file after installation or updating composer

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

All methods are static, don't need create new class object

Methods

GetRequestMethod() get request method:

$method = Request::GetRequestMethod();

switch($method) {
    case 'GET':
        // your code
        break;
    case 'POST':
        // your code
        break;
}

GET

isGet() check GET

if(Request::isGet()!== false) {
    $name = htmlspecialchars(Request::Get('name'));
}

Get($param = null) get global $_GET. If $param isn't null, method returns value of param - $_GET["param"], else - object $_GET

if(!empty(Request::Get('email'))) {
    $email = htmlspecialchars(Request::Get('email'));
}

GetParams() get all keys of $_GET, method returns array of keys

if(!in_array('user_id', Request::GetParams)) {
    echo json_encode("User ID isn't defined!");
}

POST

isPost() check POST

Post($param = null) get global $_POST. If $param isn't null, method returns value of param - $_POST["param"], else - object $_POST

PostParams get all keys of $_POST, method returns array of keys

Raw requests (php://input)

Before:

$json = file_get_contents("php://input");
$request = json_decode($json);

if(!empty($request)) {
    // your code
}

Now:

if(Request::isRaw()) {
    $request = Request::Raw();
}

isRaw() check raw request

Raw($param = null) get decoded raw request. If $param isn't null, method returns value of param, else - decoded json

RawParams get all keys of GetRawParams(), method returns array of keys

Headers

GetAllHeaders() get all headers of request, method returns array

$headers = Request::GetAllHeaders();

GetHostName() method returns host name

$domain = Request::GetHostName();

isHttps() check HTTPS

if(Request::isHttps() !== true) {
    die("Application works only with HTTPS!");
}

GetUserAgent() method returns User-Agent

$userAgent = Request::GetUserAgent();