Git Product home page Git Product logo

aura.http's Introduction

Aura HTTP

The Aura HTTP package provides objects to build and send HTTP responses from the server to the client.

Getting Started

Instantiation

The easiest way to get started is to use the scripts/instance.php script to get a new Response object.

<?php
$response = include '/path/to/Aura.Http/scripts/instance.php';

Alternatively, you can add '/path/to/Aura.Http/src' to your autoloader and build a Response object manually:

<?php
use Aura\Http\Response;
use Aura\Http\Headers;
use Aura\Http\Cookies;
$response = new Response(new Headers, new Cookies);

Setting Content

To set the content of the Response, use the setContent() method.

<?php
$html = '<html>'
      . '<head><title>Test</title></head>'
      . '<body>Hello World!</body>
      . </html>';
$response->setContent($html);

Setting Headers

To set headers, access the $headers property (which itself is a Headers collection object).

<?php
$response->headers->set('Header-Label', 'header value');

You can also set all the headers at once by passing an array of key-value pairs where the key is the header label and the value is one or more header values.

<?php
$response->headers->setAll([
    'Header-One' => 'header one value',
    'Header-Two' => [
        'header two value A',
        'header two value B',
        'header two value C',
    ],
]);

Note that header labels are sanitized and normalized, so if you enter a label header_foo it will be retained as Header-Foo.

Setting Cookies

To set cookies, access the $cookies property (which itself is a Cookies collection object). Pass the cookie name, and an array of information about the cookie (including its value).

<?php
$response->cookies->set('cookie_name', [
    'value'    => 'cookie value', // cookie value
    'expire'   => time() + 3600,  // expiration time in unix epoch seconds
    'path'     => '/path',        // server path for the cookie
    'domain'   => 'example.com',  // domain for the cookie
    'secure'   => false,          // send by ssl only?
    'httponly' => true,           // send by http/https only?
]);

The information array mimics the setcookies() parameter names. You only need to provide the parts of the array that you need; the remainder will be filled in with null defaults for you.

You can also set all the cookies at once by passing an array of key-value pairs, where the key is the cookie name and the value is a cookie information array.

<?php
$response->cookies->setAll([
    'cookie_foo' => [
        'value' => 'value for cookie foo',
    ],
    'cookie_bar' => [
        'value' => 'value for cookie bar',
    ],
]);

Setting the Status

To set the HTTP response status, use setStatusCode() and setStatusText(). The setStatusCode() method automatically sets the text for known codes.

<?php
// automatically sets the status text to 'Not Modified'
$response->setStatusCode(304);

// change the status text to something else
$response->setStatusText('Same As It Ever Was');

By default, a new Response starts with a status of '200 OK'.

Sending the Response

Once you have set the content, headers, cookies, and status, you can send the response to the client using the send() method.

<?php
$response->send();

This will send all the headers using header(), all the cookies using setcookie(), and then echo the content.

Note that you can only send the Response once. If you try to send it again, or if you try to send another response of any sort with headers on it, you will get an Exception\HeadersSent exception.

aura.http's People

Contributors

galactic-void avatar harikt 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.