Another bike: a simple library for working with HTTP requests

Hello everyone.

Working with the API is not complete without interacting with HTTP requests. Someone does not bother and uses the global arrays $ _GET, $ _POST and $ _REQUEST. Frankly, I did it myself, but not so long ago I was puzzled by the thought of the need for some kind of wrapper for ease of use. Maybe there are already such libraries, but I haven’t found them yet, except in the Bitrix API (maybe I’ve looked badly), and therefore I decided to write my own. In addition, you should agree that it is much more pleasant to use your libraries when working with code.

The library works so far with the GET and POST methods, as well as with the json construction obtained from php: // input. He also knows how to do checks on https and get headers.

Connection and call


To connect the library, use Composer:

composer require ramapriya/http-request

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

use Ramapriya\Request\Request;

Method Verification


Now you can use the library. For example, if you want to find out the type of request, call the GetRequestMethod () method :

$method = Request::GetRequestMethod();

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

However, usually the type of the method is known in advance, so in order not to create additional variables, it is enough to use the methods for checking get and post - isGet () and isPost (), respectively:

if(Request::isPost() !== false) {
   //  
} else if(Request::isGet() !== false) {
   //  
}

Receiving keys


It happens that you need to get a list of query parameters (not the values, but the keys themselves), there are also two methods for this for Get and Post:

$GetParams = Request::GetParams();

if(in_array($needle, $GetParams)) {
   //  
}

$postParams = Request::PostParams();

if(in_array($needle, $postParams)) {
   //  
}

Getting Values


And of course, there were some methods for getting the parameter values ​​themselves - Get () and Post () . The most interesting thing is that you can get both individual parameters and the entire array (which, by the way, is converted to an object - do not ask why, I just like working with objects):

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

$request = Request::Post();
if(Request::isPost() && !empty($request)) {
   //  
}

Raw requests (php: // input)


We should also dwell on the methods of working with php: // input. This isRaw () - checks for a raw request, Raw () , which returns the json string converted to an object, and RawParams () , which returns the request keys. I remember when I was working with the Sendpulse API, I had to write something like this:

$rawRequest = file_get_contents('php://input');
$request = json_decode($rawRequest);

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

Of course, one variable could be dispensed with.
$request = json_decode(file_get_contents('php://input'));

But you must admit, it looks rather confusing.

With the Raw () and isRaw () methods , the code already causes more aesthetic pleasure:

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

Work with headers


There are also several methods for working with headers in the library:

GetAllHeaders () - getting all headers.
$headers = Request::GetAllHeaders();

GetHostName () - getting the host name
$domain = Request::GetHostName();

isHttps () - check for https

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

GetUserAgent () - getting user agent. This can be important for someone.

$userAgent = Request::GetUserAgent();

The library will be supplemented and modified. Source code, as always, on github

Thank you for your attention.

All Articles