PHP HTTP Request Wrapper
Class for encapsulating GET, POST, and FILE requests
This is a tiny class that encapsulates HTTP requests in a read-only format. It allows you to access request parameters by name regardless of the request method, and set a default return value for parameters that were not found in the request.
The PHP superglobal $_REQUEST includes the contents of $_GET, $_POST and $_COOKIE, but I prefer to include the contents of $_FILES and exclude $_COOKIES from my request object. You can easily modify the class to your own liking.
<?php
class Request
{
public function getParameter($param, $return=null)
{
if (isset($_POST[$param]))
{
$return = $_POST[$param];
}
else if (isset($_GET[$param]))
{
$return = $_GET[$param];
}
else if (isset($_FILES[$param]))
{
$return = $_FILES[$param];
}
return $return;
}
}
Basic usage:
$request = new Request();
$id = $request->getParameter("id");
Set default value for missing parameters:
$checkbox = $request->getParameter("checkbox", 0);
Get uploaded file:
$form_data = $request->getParameter('form_data');
if (!is_null($form_data) && $form_data['tmp_name'] != '')
{
$file_name = $form_data['name'];
$file_type = $form_data['type'];
$file_size = $form_data['size'];
$tmp_name = $form_data['tmp_name'];
}

