faraweilyas/mangoapi

PHP library that provides an object-oriented wrapper to manage API requests.

dev-master 2019-07-09 19:46 UTC

This package is auto-updated.

Last update: 2024-11-10 08:19:38 UTC


README

PHP library to handle API requests and send responses.

Requirements

Install with composer

The best way to add the library to your project is using composer.

composer require faraweilyas/mangoapi

or

Clone this repo

git clone https://github.com/faraweilyas/mangoapi.git

Basic usage

<?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();

Handling Request

$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();

Sending Response

  • 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();