Skip to content

hrishikesh214/php-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Easily create RESTFull API in PHP

It supports only JSON but you can change output :-) 

Index

  1. Installation
  2. Documentation
  3. Creating Endpoint
  4. Passing URL Parameters
  5. Post Parameters
  6. API Tracer
  7. Setting Error Handlers
  8. External Routes

Installation

composer require hrishikesh214/php-api

And then in your file

require 'vendor/autoload.php';

Configurations

Before getting into PHPAPI we have to make some configs

In your root directory, create .htaccess file and paste following code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?URL=$1 [L]

Documentation

Creating a client

$client = new phpapi\Client();

You can also pass base address

$client = new phpapi\Client("api/");

All child endpoints will be access after api/

Running Client

print_r($client->run(isset($_GET['URL']) ? $_GET['URL'] : ""));

This $client->run() returns response string, so you can store it in variable for further process or directly print it as response

Creating an Endpoint

$client->mount('GET', 'token', function(){
    //some calculations
    return $token;
});

Only some request methods are allowed : 'PUT', 'POST', 'DELETE', 'PATCH', 'GET', 'PURGE'

mount returns boolean value about whether endpoint is mounted successfully or not

Passing URL Parameters

Client will automatically pass url parameters to the response function

example is given below

$client->mount('GET', 'wish/:name/:age', function($props){
    return "Hi $props['name'], you are $props['age] years old!";
});

URI Parameters are directly passed to the responder function in an associative array according to the key passed in parameter

All other posted parameters will automatically get stored in $_POST

NOTE If parameter is define while mounting but not passes then it is given a null value

POST Endpoint

$respond = function(){
    return $_POST; //It will have all posted data!
};
$client->mount("POST", 'checkpost', $respond);

Trace Whole API

You can trace whole API Client with detailed configs of all endpoints mounted with a function

$client->trace(true|false);

If Above value is true final api result will also included api configs

This Also includes Requested configs

Example output:-

{
  "track": {
    "routes": {
      "POST": {
        "api/wish": {
          "base": "api/wish",
          "type": "POST",
          "params": [ ]
        },
        "name": {
          "base": "name",
          "type": "POST",
          "params": [ ]
        }
      },
      "GET": {
        "api/msg": {
          "base": "api/msg",
          "type": "GET",
          "params": [ ]
        },
        "api/wish": {
          "base": "api/wish",
          "type": "GET",
          "params": {
            "name": 3
          }
        },
        "name": {
          "base": "name",
          "type": "GET",
          "params": {
            "name": 3
          }
        },
        "/": {
          "base": "/",
          "type": "GET",
          "params": [ ]
        }
      }
    },
    "base": "",
    "request_blocks": [
      "api",
      "msg"
    ],
    "request_uri": "api/msg",
    "request_type": "GET"
  },
  "result": "trial"
}

In above example result will contain result coming from API

Setting Request Errors

There is a default error handler but you change it!

404

$client->set404([
    'error_type' => 404,
    'error_msg' => "Not Found"
]);

405

$client->set405([
    'error_type' => 405,
    'error_msg' => "Method Not Allowed"
]);

External Routes

Importing Routes from external file

You can also define routes in external file all you need is to use Helper Class.

For example (folder structure):-

|   myRoutes
|       - api.php
|   vendor (composer files)
|   index.php
//index.php
$client = new phpapi\Client();
$helper = new phpapi\Helper($client);

$helper->use('myRoutes/api.php' [, basename: 'api']);
// myRoutes/api.php

// You can define functions and also pass to callback
$myFunc = function($props){
            return "Good morning {$props['name']}";
        };

$routes = [
    [
        "match" => 'msg',
        "type" => "get",
        "callback" => function(){
            return "trial";
        }
    ],
    [
        "match" => 'wish/:name',
        "type" => "get",
        "callback" => $myFunc
    ],
    [
        "match" => 'wish',
        "type" => "post",
        "callback" => function(){
            return "Good morning {$_POST['name']}";
        }
    ]
];

$config = [
    'base' => 'api'
];

In above code $config['base']' will be act as base to all routes present in this file.

$routes will contain all routes.

Please maintain format else code will not work!

Made with ❤️By Hrishikesh

About

Easily build restful API in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages