Giter Site home page Giter Site logo

http's Introduction

Http Component

Build Status Coverage Status Latest Stable Version Total Downloads License

Installation

You can use composer to install this component:

composer require tflori/http

Basic Usage

Request

The Request class provides an object oriented wrapper around the PHP superglobals. This makes it possible to inject it as a dependency into any of your classes that require it.

use Http\HttpRequest;

$request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input'));
// equals to:
$request = HttpRequest::createFromGlobals();

Now you can use the following methods on the $request object:

$request->getParameter($key, $defaultValue = null);
$request->getFile($key, $defaultValue = null);
$request->getCookie($key, $defaultValue = null);
$request->getParameters();
$request->getQueryParameters();
$request->getBodyParameters();
$request->getRawBody();
$request->getCookies();
$request->getFiles();
$request->getMethod();
$request->getScheme();
$request->getHttpAccept();
$request->getReferer();
$request->getUserAgent();
$request->getIpAddress();
$request->isSecure();
$request->getQueryString();

Please note that both GET and POST parameters are merged together and accessible with getParameter.

Response

The HttpResponse object is the data holder for the HTTP response. It has no constructor dependencies and can be instantiated with just:

use Http\HttpResponse;

$response =  new HttpResponse;

The response can be modified with following methods:

$response->setStatusCode($statusCode, $statusText = null);
$response->addHeader($name, $value);
$response->setHeader($name, $value);
$response->addCookie(Cookie $cookie);
$response->deleteCookie(Cookie $cookie);
$response->setContent($content);
$response->redirect($url);

If you don't supply a status text with setStatusCode then an appropriate default status text will be selected for the HTTP status code if available.

addHeader adds a new header value without overwriting existing values, setHeader will overwrite an existing value.

The redirect method will set the status code and text for a 301 redirect.

deleteCookie will set the cookie content to nothing and put the expiration in the past.

The following two methods are available to get the current data in the response:

$response->getHeaders();
$response->getContent();

To send the response use the following method:

$response->send();

make sure not to send the response twice as you will get an error message.

Cookies

To avoid new calls in your classes and to have the ability to set default cookie settings for you application, there is a CookieBuilder class that you can use to create your cookie objects. It has the following methods available:

$cookieBuilder->setDefaultDomain($domain); // defaults to NULL
$cookieBuilder->setDefaultPath($path); // defaults to '/'
$cookieBuilder->setDefaultSecure($secure); // defaults to TRUE
$cookieBuilder->setDefaultHttpOnly($httpOnly); // defaults to TRUE
$cookieBuilder->build($name, $value); // returns the cookie object

You can use the following methods to manipulate an existing cookie:

$cookie->setValue($value);
$cookie->setMaxAge($seconds);
$cookie->setDomain($domain);
$cookie->setPath($path);
$cookie->setSecure($secure);
$cookie->setHttpOnly($httpOnly);

The cookie object can the be used with the HttpResponse methods addCookie and deleteCookie.

Example

<?php

use Http\HttpRequest;
use Http\HttpResponse;
use Http\CookieBuilder;

$loader = require_once __DIR__ . '/vendor/autoload.php';

$cookieBuilder = new CookieBuilder();

// Disable the secure flag because this is only an example
$cookieBuilder->setDefaultSecure(false);

$request = HttpRequest::createFromGlobals();
$response = new HttpResponse();

$content = '<h1>Hello World</h1>';
$content .= $request->getCookie('TestCookie', 'Cookie is not set.');

if ($request->getParameter('setCookie') === 'true') {
    $cookie = $cookieBuilder->build('TestCookie', 'Cookie is set.');
    $response->addCookie($cookie);
}

$response->setContent($content);

$response->send();

http's People

Contributors

fabieno avatar hassanalthaf avatar iroegbu avatar nicoqh avatar samayo avatar tflori avatar vlakarados avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.