weew/http

Basic http layer.

v1.13.0 2016-11-03 17:04 UTC

README

Build Status Code Quality Test Coverage Version Licence

Table of contents

Installation

composer require weew/http

Responses

Basic response

$response = new HttpResponse();
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close

Content

$response = new HttpResponse();
$response->setContent('<h1>Hello World!</h1>');
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: text/html

<h1>Hello World!</h1>

Status codes

$response = new HttpResponse(HttpStatusCode::UNAUTHORIZED);
// or
$response = new HttpResponse(401);
$response->send();
HTTP/1.1 401 Unauthorized
Host: localhost
Connection: close

Headers

$response = new HttpResponse();
$response->getHeaders()->set('foo', 'bar');
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
foo: bar

Cookies

$response = new HttpResponse();
$response->getQueuedCookies()->add(new Cookie('foo', 'bar'));
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
set-cookie: foo=bar; path=/; httpOnly

Custom responses

HtmlResponse

$response = new HtmlResponse();
$response->setHtmlContent('<h1>Hello World!</h1>');
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: text/html

<h1>Hello World!</h1>

JsonResponse

$response = new JsonResponse();
$response->getData()->set('Hello', 'World!');
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: application/json

{"Hello":"World!"}

BasicAuthResponse

$response = new BasicAuthResponse('Please login');
$response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
www-authenticate: basic realm="Please login"

Requests

Basic request

It is very easy to build a custom request.

$request = new HttpRequest(
    HttpRequestMethod::POST,
    new Url('http://example.com')
);
$request->setContent('foo=bar');

GET parameters

$request = new HttpRequest();
$request->getUrl()->getQuery()->set('foo', 'bar');

echo $request->getUrl()->getQuery();
// foo=bar

POST data

$request = new HttpRequest();
$request->getData()->set('foo', 'bar');
$request->getData()->set('bar', 'foo');

echo $request->getContent();
// foo=bar&bar=foo

Headers

You can access headers the same way as on the HttpResponse class.

$request = new HttpRequest();
$request->getHeaders()->set('foo', 'bar');
$request->getHeaders()->add('foo', 'foo');

var_dump($request->getHeaders()->get('foo'));
// ['bar', 'foo']
echo $request->getHeaders()->find('foo');
// foo

Current Request

Sometimes it is nice to have an object that would represent the current received http request.

$request = new CurrentRequest();
var_dump($request->toArray());
// all the data that the server received from the client

Basic Authentication

It is very easy to authenticate a request via basic auth.

$request = new HttpRequest();
$request->getBasicAuth()->setUsername('foo');
$request->getBasicAuth()->setPassword('bar');
echo $request->getBasicAuth()->getToken();
// Zm9vOmJhcg==
echo $request->getHeaders()->find('authentication');
// Basic Zm9vOmJhcg==

Related Projects

  • URL: used throughout the project.
  • HTTP Client: a simple http client that allows you to send and receive the standardized HttpRequest and HttpResponse objects.