Git Product home page Git Product logo

php-rest-api-lib's Introduction

A simple PHP Rest API library

This library helps you to create a rest API by adding convenient tools and structure to the PHP code-base.

Getting started

An example of how you can configure the rest API. In the root folder of where you want your API to resinde at the PHP server create a .htaccess file that rewrites all subtrafic to this file.

RewriteEngine On
RewriteRule ^(.*) index.php?_=$1 [QSA,L]

Next create the index.php file with the API endpoints you would want to use.

use \gd\rest\App;
use \gd\rest\Request;
use \gd\rest\Response;
$app = new App();
$app->setRequestPathName('/' . $_GET['_']);
unset($_GET['_']);

// Declare endpoints:
$app->get('/api/v1/ping', function (Request $request, Response $response) {
    $response->end('pong');
  })
  ->get('/api/v1/short-url', function (Request $request, Response $response) {
    $response->redirectTo($request->getUrl() . '/api');
  })
  ->get('/api/v1/api', function (Request $request, Response $response) {
    include('docs/index.php'); // will serve the documentation with it's own logic
  })
  ->get('/app/content', function (Request $request, Response $response) {
    $response->end(file_get_contents('content/index.html'));
  })
  ->use('/app', '../static/pages') // Serve normal files as a file server
  ->post('api/v1/update, authenticate, function (Request $request, Response $response) {
    $params = $request->getPostParams([
      'id' => [ 'match' => '/\d+/', 'required' => true, 'default' => null ]
    ]);
    if ($params === false) $response->withStatus(400)->end('Invalid params, id must be numeric!');
    // TODO: update id =)
    $response->withStatus(200)->end('Successfully updated id');
  });

Additional features supported are file uploading, put and delete requests as well as request chaining and forwarding.

php-rest-api-lib's People

Contributors

jolin1337 avatar

Watchers

James Cloos avatar  avatar

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.