Skip to content

PHP library to handle API requests and send responses

License

Notifications You must be signed in to change notification settings

faraweilyas/mangoapi

Repository files navigation

mangoapi

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

About

PHP library to handle API requests and send responses

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages