Download[PHP] Pharaoh HTTP
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>
Get the request's method:
echo $request->method; //GET or POST or PUT or PATCH or DELETE
<a name="status"></a>
Get the request's status code:
echo $request->status; //200
<a name="secured"></a>
Is it a secured request (HTTPS) (Returns Boolean) :
echo $request->secured; //true
<a name="time"></a>
Get the request's time :
echo $request->time; //float
//OR int
echo (int)$request->time;
<a name="headers"></a>
Get the header value of a given key:
echo $request->headers->host; //localhost
<a name="server"></a>
Get the server value of a given key:
echo $request->server->http_host; //localhost
<a name="url"></a>
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>
// 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>
Check if the request method is equal to the given method name:
echo $request->isMethod('PUT') // false
<a name="isGet"></a>
Check if it is a GET request:
echo $request->isGet() // true
<a name="isPost"></a>
Check if it is a POST request:
echo $request->isPost() // false
<a name="isPut"></a>
Check if it is a PUT request:
echo $request->isPut() // false
<a name="isPatch"></a>
Check if it is a PATCH request:
echo $request->isPatch() // false
<a name="isDelete"></a>
Check if it is a DELETE request:
echo $request->isDelete() // false
<a name="isAjax"></a>
Check if it is a XML Http Request:
echo $request->isAjax() // false
<a name="query"></a>
Get a query value/values using DotArray:
// localhost/pharaoh/http/?name=Raggi
echo $request->query('name') // Raggi
<a name="input"></a>
Get a post value/values using DotArray:
echo $request->input('full_name') // Moamen Eltouny
<a name="hasQuery"></a>
Check if the request has the given query key using DotArray:
echo $request->hasQuery('full_name') // false
<a name="hasInput"></a>
Check if the request has the given input key using DotArray:
echo $request->hasInput('full_name') // true
<a name="client"></a>
$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>
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>
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 content or append to it or prepend to it:
$response->set('<h1>content</h1>');
$response->append('FOOTER');
$response->prepend('HEADER');
<a name="json"></a>
Set JSON output:
$response->json([
'data' => [
[
'name' => 'Moamen Eltouny',
'nickname' => 'Raggi'
]
]
]);
/*
{
"data": [
{
"name": "Moamen Eltouny",
"nickname": "Raggi"
}
]
}
*/
<a name="variables"></a>
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
|