weew / http
Basic http layer.
v1.13.0
2016-11-03 17:04 UTC
Requires
- weew/contracts: ^1.1
- weew/helpers-array: ^1.0
- weew/helpers-string: ^1.0
- weew/json-encoder: ^1.0
- weew/url: ^2.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
- dev-master
- v1.13.0
- v1.12.0
- v1.11.0
- v1.10.1
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is not auto-updated.
Last update: 2024-11-23 20:04:32 UTC
README
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.