https://github.com/alexsasharegan/http_lib
An ultra simple & lightweight library for making file-based API's easier in PHP.
https://github.com/alexsasharegan/http_lib
callback json micro-framework micro-service php rest-api
Last synced: 2 months ago
JSON representation
An ultra simple & lightweight library for making file-based API's easier in PHP.
- Host: GitHub
- URL: https://github.com/alexsasharegan/http_lib
- Owner: alexsasharegan
- Created: 2016-08-23T07:34:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-18T08:00:52.000Z (over 8 years ago)
- Last Synced: 2025-07-01T22:06:41.694Z (12 months ago)
- Topics: callback, json, micro-framework, micro-service, php, rest-api
- Language: PHP
- Homepage:
- Size: 240 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Http
[](https://packagist.org/packages/alexsasharegan/http)
[](https://packagist.org/packages/alexsasharegan/http)
[](https://packagist.org/packages/alexsasharegan/http)
[](https://packagist.org/packages/alexsasharegan/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 the `Http_Autoloader.php`.
```php
post('myPostCallback');
```
#### Inline Closure
```php
post(
function ($http) {
# code ...
}
);
# also possible
$myGlobalVar = [1,2,3];
(new Http)
->get(
function ($http) {
# code ...
}
)
->post(
function ($http, $myGlobalVar) {
# code ...
}
, $myGlobalVar
)
->exec();
```
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
```json
{
"error": "No route has been defined for this request method."
}
```
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:
```php
exec();
```
## Examples
```php
'1.1.1.1',
'databaseName' => 'myDatabase',
'dbUserName' => 'admin',
'dbPassword' => 'adminPass',
]);
# make a select query and pull $id from the request query string
$db->query(
"SELECT * FROM `sellers` WHERE `id` = {$http->request->query('id')}"
)
# this func gets called once for each row
# 'use' pulls in $http from the closure's parent scope
# sometimes we need to pass by reference like this: use( &$var )
->iterateResult(
function ( $row ) use ( $http ){
$http->response->set_array($row);
}
);
# nesting operations to dumb the column names into response['sellers']
$http->response->set( 'sellersColumns', $db->getColumns('sellers') );
# test setting different types
$http->response->set( 'test', [
'one' => 1,
'two' => 'two',
'three' => true,
'four' => [1,2,3],
]);
# send what's in our response object
$http->send();
}
# make our instance of Http\Http
$http = new Http;
# chain our calls together
$http
->get( 'get' )
->post(
function ( $http ) {
# code ...
}
)
# there is a default exception handler,
# but you can set a custom exception handler
# like this:
->error(
function ( Exception $e ) use ( $http ) {
$http->send(500, 'text/html', new Html('code', $e));
}
)
# execute the route
->exec();
```
```php