PHP library
Http Requester Class in PHP
Author: Thorpe Lee(koangbok@gmail.com), Front&Back-end Developer, posted on 2015-12-10 03:18:01
SimpleRequest is a php library that helps you quickly get the value of http request. To use this library is very esay and simple so you can customize the code if you want. Thank you for using the library.
How to Install
using the Composer (http://getcomposer.org/), If you want to get more information about the Composer, visit here("https://xpressengine.github.io/Composer-korean-docs/"), This is Korean site
Create a composer.json file in your project root:
{
"require": {
"cafelatte/endecrypter": "1.0.0"
}
}
How to use
If you use the Composer that is a tool for dependency management in PHP, put the below code in your phps.
request = new SimpleRequest();
$value = $aa->request->post('getkey');
var_dump($value);
$value = $aa->request->get('getkey');
var_dump($value);
$value = $aa->request->cookie('getkey');
var_dump($value);
$value = $aa->request->file('image');
var_dump($value);
$value = $aa->request->header('image');
var_dump($value);
Server Requirements
- PHP >= 5.3
- MCrypt PHP Extension
As of PHP 5.5, some OS distributions may require you to manually install the PHP Mcrypt extension. When using Ubuntu, this can be done via apt-get install php5-mcrypt.
How to use
If you use the Composer that is a tool for dependency management in PHP, put the below code in your phps.
* @version 1.0
* @copyright Copyright (c) 2014, Thorpe Lee
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @date 2014.05.26
*/
class SimpleRouter
{
private $routes = array();
private $requestMethod;
private $uri;
private $serverName;
/**
* __construct
*/
public function __construct()
{
$this->requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_SPECIAL_CHARS);
$this->uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_SPECIAL_CHARS);
$this->serverName = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_SPECIAL_CHARS);
}
/* -------------------------------------- */
/**
* allow to access via the get request
*
* @param type $pattern
* @param type $callback
*/
public function get($pattern, $callback)
{
if ($this->requestMethod === "GET") {
$this->mapRoute($pattern, $callback);
}
}
/**
* allow to access via the post request
*
* @param type $pattern
* @param type $callback
*/
public function post($pattern, $callback)
{
if ($this->requestMethod === "POST") {
$this->mapRoute($pattern, $callback);
}
}
/**
* allow to access via the delete request
*
* @param type $pattern
* @param type $callback
*/
public function delete($pattern, $callback)
{
if ($this->requestMethod === "DELETE") {
$this->mapRoute($pattern, $callback);
}
}
/**
* allow to access via the delete request
*
* @param type $pattern
* @param type $callback
*/
public function put($pattern, $callback)
{
if ($this->requestMethod === "PUT") {
$this->mapRoute($pattern, $callback);
}
}
/**
* map
*
* @param string $pattern
* @param type $callback
*/
private function mapRoute($pattern, $callback)
{
$word = self::getPattern($pattern);
$pattern = '/^' . str_replace('/', '\/', $word) . '$/';
$this->routes[$pattern] = $callback;
}
/**
* input patten re change
*
* @param type $pattern
* @return string
*/
private static function getPattern($pattern)
{
$keywords = preg_split("/\//", $pattern);
foreach ($keywords as $keyword) {
$i++;
if (preg_match("/:/i", $keyword)) {
$word .= "([a-zA-Z0-9-]+)";
} else {
$word .= $keyword;
}
if (count($keywords) != $i) {
$word .= "/";
}
}
return $word;
}
/**
* userCallable
* @param type $callback
* @param type $params
* @return type
*/
private static function userCallable($callback, $params)
{
return call_user_func_array($callback, array_values($params));
}
/**
* execute
*/
public function run()
{
$base = str_replace('\\', '/', dirname($this->serverName));
if (strpos($this->uri, $base) === 0) {
$url = substr($this->uri, strlen($base));
}
foreach ($this->routes as $pattern => $callback) {
if (preg_match($pattern, $url, $params)) {
array_shift($params);
self::userCallable($callback, $params);
}
}
}
}
License
MIT Public License