PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Moamen Eltouny   Pharaoh HTTP   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Pharaoh HTTP
Get and set the values of the current HTTP request
Author: By
Last change:
Date: 4 years ago
Size: 7,728 bytes
 

Contents

Class file image Download

[PHP] Pharaoh HTTP

Latest Stable Version Total Downloads License

Pharaoh-HTTP provides a quick and easy controlling of Request and Response.

Install

Install the latest version using Composer:

$ composer require raggitech/pharaoh-http

the include the vendor autoload file.

Usage

Getting the Instances:

$request = \RaggiTech\Http\Request::getInstance();
$response = \RaggiTech\Http\Response::getInstance();

Request

<a name="method"></a>

method

Get the request's method:

echo $request->method; //GET or POST or PUT or PATCH or DELETE

<a name="status"></a>

status

Get the request's status code:

echo $request->status; //200

<a name="secured"></a>

secured

Is it a secured request (HTTPS) (Returns Boolean) :

echo $request->secured; //true

<a name="time"></a>

time

Get the request's time :

echo $request->time; //float

//OR int
echo (int)$request->time;

<a name="headers"></a>

headers

Get the header value of a given key:

echo $request->headers->host; //localhost

<a name="server"></a>

server

Get the server value of a given key:

echo $request->server->http_host; //localhost

<a name="url"></a>

url

echo $request->current(); // localhost/pharaoh/http/?name=Raggi

// get current url with scheme
echo $request->current(true); // http://localhost/pharaoh/http/?name=Raggi

// get the base url
echo $request->base(); // http://localhost

// get the route url
echo $request->forRoute(); // localhost/pharaoh/http

?

<a name="files"></a>

files (uploaded files)

// for example the file input is FN.

########################################
# File Object
########################################
// single file
$file = $request->file('FN');
// OR
$file = $request->files->FN;

// Multi files (name, index)
$file = $request->file('FN', 1);
// OR 
$file = $request->files->FN[1];

########################################
# File Properties
########################################

echo $file->name; // name
echo $file->extension; // extension
echo $file->type; // mime type
echo $file->path; // current path
echo $file->error; // error code

########################################
# File Methods
########################################

// get full name with extension
echo $file->fullName(); // image.jpg

// check if the file has no error
echo $file->isValid(); // true

// get readable size of the file
echo $file->readableSize(); // 2MB

// save uploaded file to path
$file->save('path'); // true

// or save uploaded file to path with a new name
$file->save('path', 'file.jpg'); // true

<a name="isMethod"></a>

isMethod()

Check if the request method is equal to the given method name:

echo $request->isMethod('PUT') // false

<a name="isGet"></a>

isGet()

Check if it is a GET request:

echo $request->isGet() // true

<a name="isPost"></a>

isPost()

Check if it is a POST request:

echo $request->isPost() // false

<a name="isPut"></a>

isPut()

Check if it is a PUT request:

echo $request->isPut() // false

<a name="isPatch"></a>

isPatch()

Check if it is a PATCH request:

echo $request->isPatch() // false

<a name="isDelete"></a>

isDelete()

Check if it is a DELETE request:

echo $request->isDelete() // false

<a name="isAjax"></a>

isAjax()

Check if it is a XML Http Request:

echo $request->isAjax() // false

<a name="query"></a>

query()

Get a query value/values using DotArray:

// localhost/pharaoh/http/?name=Raggi
echo $request->query('name') // Raggi

<a name="input"></a>

input()

Get a post value/values using DotArray:

echo $request->input('full_name') // Moamen Eltouny

<a name="hasQuery"></a>

hasQuery()

Check if the request has the given query key using DotArray:

echo $request->hasQuery('full_name') // false

<a name="hasInput"></a>

hasInput()

Check if the request has the given input key using DotArray:

echo $request->hasInput('full_name') // true

<a name="client"></a>

client

$client = $request->client;

##############################
# Bot
##############################
// check if it's a bot
var_dump($client->isBot());

// get the bot's name
echo $client->bot();

##############################
# Main Information
##############################

// get user agent
echo $client->agent;

// get user ip
echo $client->ip;

// get referer
echo $client->referer;

// get user languages list
echo $client->languages;

// get user language
echo $client->language;

// get user language's variant
echo $client->variant;

##############################
# Device
##############################
$device = $client->device;

// Browser Engine name
echo $device->name; // WebKit

// Browser (name, version)
echo $device->browser->name; // Chrome
echo $device->browser->version; // 77.0.3865.120

// Platform (name, version)
echo $device->platform->name; // Windows
echo $device->platform->version; // 10.0


// Device Type
var_dump($device->isDesktop); // true

// if it's a phone
if($device->isPhone){
    var_dump(
        $device->isMobile, // true
        $device->isTablet, // false
        
        $device->isiOS, // true
        $device->isAndroid, // false
    );
}

Response

<a name="status"></a>

status()

Get/Set the response's status code:

echo $response->status(); //200

$response->status(404);
echo $response->status(); //404

<a name="setHeader"></a>

<a name="setHeaders"></a>

setHeader() & setHeaders()

Get header or multi headers:

$response->setHeader('Location', 'https://raggitech.com');

$response->setHeaders([
    'Content-Type: application/pdf',
    'Content-Disposition: attachment; filename="downloaded.pdf"',
    'original.pdf',
]);

<a name="set"></a>

<a name="append"></a>

<a name="prepend"></a>

set() & append() & prepend()

Set content or append to it or prepend to it:

$response->set('<h1>content</h1>');

$response->append('FOOTER');
$response->prepend('HEADER');

<a name="json"></a>

json()

Set JSON output:

$response->json([
    'data' => [
        [
            'name' => 'Moamen Eltouny',
            'nickname' => 'Raggi'
        ]
    ]
]);

/*
{
    "data": [
        {
            "name": "Moamen Eltouny",
            "nickname": "Raggi"
        }
    ]
}
*/

<a name="variables"></a>

variables

Get/Set Variables:

$response->page_title = "RaggiTech";
echo $response->page_title; // RaggiTech

var_dump($response->getVariables());

$response->append('FOOTER');
$response->prepend('HEADER');

License

MIT license