Download the PHP package alexsasharegan/http without Composer
On this page you can find all versions of the php package alexsasharegan/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexsasharegan/http
More information about alexsasharegan/http
Files in alexsasharegan/http
Package http
Short Description An ultra simple & lightweight library for making RESTful api's easier in PHP.
License MIT
Homepage https://github.com/alexsasharegan/Http_lib
Informations about the package http
Http
A lightweight, dependency free library that makes writing file-based RESTful JSON API endpoints easier in PHP.
Setup
Clone the repo into your project. Assuming your restful endpoints live in an /api
directory, I would recommend either making an /api/vendor
folder or just a plain /api/libs
folder and cloning this repo inside there.
- In your project,
require_once
the path to theHttp_Autoloader.php
.
Instantiation
Properties
Properties for class Http
represented in json:
Some example properties for the class Http\Request
represented in json (they vary based on the request itself):
Properties for Http\Reponse
are private. This allows the response object to manage the response data and serialize it on send.
Methods
The Http
class has a method available for each major HTTP verb (get, post, put, patch, delete). These allow you to attach your callbacks to be run on each appropriate request method. You can pass in the string name of your callback, or write your function inline as a closure. The callback will be called with the instance of Http
.
Callback Reference
Inline Closure
To get values off the parsed request body, call Http\Request::get( string $key )
.
When writing your callbacks, you can build up your response with two methods:
-
Http\Response::set( string $key, mixed $value )
Parameters
- key: the name for the value you wish to set
- value: the value you wish to set
-
Http\Response::set_array( array $array )
Parameters
- array: an associative array of values to set on the response
The last line in your callback will be a call to Http::send
. This exits execution completely after sending the response.
-
Http::send( [ int $statusCode = 200, string $contentType = "application/json", string $content = '' ] )
Parameters
- statusCode: a valid HTTP status code to return
- contentType: a valid MIME Type to set the response header
- content: if you set Content-Type to something other than json, you can send your custom data with this parameter. No serialization will be performed on this content.
- Note: any undefined routes will return a status code
405
with a json formatted error message
If you use a try {} catch(Exception $e) {}
block in your error handling, you can call Http::handleError( Exception $e )
in your catch block, and it will automatically reply with a 500
code and a json payload containing the error.
Once you have defined all your necessary HTTP method callbacks, you can let your instance of Http
run the appropriate callback by simply calling: