An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Http
[![Latest Stable Version](https://poser.pugx.org/alexsasharegan/http/v/stable)](https://packagist.org/packages/alexsasharegan/http)
[![Total Downloads](https://poser.pugx.org/alexsasharegan/http/downloads)](https://packagist.org/packages/alexsasharegan/http)
[![Latest Unstable Version](https://poser.pugx.org/alexsasharegan/http/v/unstable)](https://packagist.org/packages/alexsasharegan/http)
[![License](https://poser.pugx.org/alexsasharegan/http/license)](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