Giter Club home page Giter Club logo

mangoapi's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.