PHP library to handle API requests and send responses.
- PHP version 7.0
- composer.
The best way to add the library to your project is using composer.
composer require faraweilyas/mangoapi
or
git clone https://github.com/faraweilyas/mangoapi.git
<?php
use MangoAPI\Request;
use MangoAPI\Response;
require_once 'vendor/autoload.php';
// Handling request
$request = new Request();
$request->addHeader("Access-Control-Allow-Origin", "*")
->addHeader("Content-Type", "application/json; charset=UTF-8")
->setAllowMethods(["GET", "POST", "PUT", "HEAD", "DELETE", "PATCH", "OPTIONS"])
->addHeader("Access-Control-Max-Age", "3600")
->addHeader("Access-Control-Allow-Credentials", "true")
->addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
->setHeaders();
// Make database calls or any other operations
// Sending response
$response = new Response;
$data = ['username' => 'hadiza123', 'name' => 'Agbonoga Hadiza', 'sex' => 'female'];
$response->isOk("Successfully processed", $data)
->send()
->kill();
$request = new Request();
// Add header 1
$request->addHeader("Access-Control-Allow-Origin", "*");
// Add header 2
$request->addHeader("Access-Control-Max-Age", "3600");
// Setting header
$request->setHeaders();
// Get raw data from request
$data = $request->getDataFromRequest();
// Get headers
$headers = $request->getHeaders();
- Send 200 ok response
$response = new Response;
$data = ['username' => 'hadiza123', 'name' => 'Agbonoga Hadiza', 'sex' => 'female'];
$response->isOk("Successfully processed", $data)
->send()
->kill();
- Send 201 created response
$response = new Response;
$data = ['username' => 'hadiza123', 'name' => 'Agbonoga Hadiza', 'sex' => 'female'];
$response->isCreated("Successfully created", $data)
->send()
->kill();
- Send 400 bad request response
$response = new Response;
$data = [];
$response->badRequest("Request can't be processed", $data)
->send()
->kill();
- Send 404 not found response
$response = new Response;
$response->notFound("Request not found")
->send()
->kill();
- Send 422 unprocessable entity response
$response = new Response;
$response->unProcessableEntityResponse()
->send()
->kill();