Giter Club home page Giter Club logo

php_rest_api's Introduction

LTS+sub-branches

PHP REST API

Actually, this repository provides a generic way to implement API (so not only REST API). Tested/used in PHP <5.3. The expected directory structure is:

api/
	this_repo/
	_config.php … see below
	index.php … or any other entry php file
	.htaccess

The API entry is splitted into several steps:

  1. config … see AbstractConfig.php
  2. request processing … see for exmaple RequestHTTP.php, RequestServer.php
  3. special cases … see for exmaple AuthorizationJWT.php, HelpREST
  4. the actual processing of the request … see apiREST.php
  5. response … see for example ResponseJSON

Use in your repo

Use:

cd TARGET_PATH
git submodule add -b main --depth=1 [email protected]:IndigoMultimediaTeam/php_rest_api _internal

… more info git submodule.

Target folder (_internal) schouldn’t be accessible from outside of the web!

Minimal REST API example whith authentication

_config.php
<?php
/* import { AbstractConfig } from */require_once '_internal/AbstractConfig.php';
class config extends AbstractConfig{
	public $secret= 'secret for authentication/authorization';
	/** Result of authentication */
	public $client;
	public $versions= array(
		'warty-warthog',
		'hoary-hedgehog',
		'dapper-drake'
	);
}
index.php
<?php
require_once '../../kernel/kernel.php';//fix path
require_once '../utils/inc.db_utils.php';//fix path
/* import { Config } from */require_once '_config.php';
$config= new config();
$config->api_url= 'https://'.$_SERVER['HTTP_HOST'].'/api/rest';
/* import { Request } */require_once '_internal/RequestHTTP.php';
$request= new Request($config);
// api logging if needed, see later
/* import { Response } */require_once '_internal/ResponseJSON.php';
$response= new Response($request);
// special help endpoints, see later
/* import { Authorization } */require_once '_internal/AuthorizationJWT.php';
$auth= new Authorization($config, $request, $response);
$config->client= $auth->jwt_playload;

/* import { api } */require_once '_internal/apiREST.php';
$response->phase('[2] Request processing');
					 try{	 return $response->success(api($config, $request)); }
catch(\Exception $error){	 return $response->error($error); }

… for authorization via AuthorizationJWT.php you need to provide auth folder (see AbstractConfig->$auth_path):

api/
	this_repo/
	…
	auth/
		token/
			post.php
		authorize/
			post.php
		update/
			post.php

… this is similar to REST API handlering via apiREST.

The folder structure follows the requested URL:

https://api_url/api_version/Folder/…

folder:
api/
	this_repo/
	Folder/…

…request method indicates which file to use:

curl -X POST https://api_url/api_version/Folder

file:
folder:
api/
	this_repo/
	Folder/post.php

For more extended version visits DHLC-Internet-Networking/web/api/rest at dev/php_rest_api · jaandrle/DHLC-Internet-Networking.

Minimal version of making API accessible from inside the server

<?php
/**
 * @param string $get Target URL `version/target`
 * @param "get"|"delete"|"put"|"post" $method
 * @param array<string, mixed> $body
 * */
function api($get, $method= 'get', $body= array()){
	$cwd= $GLOBALS['__dROOT'].'api/rest/';
	require_once $cwd.'_internal/libs/globals.php';
	/* import { Config } from */require_once $cwd.'_config.php';
	$config= new config();
	$config->api_url= 'https://'.$_SERVER['HTTP_HOST'].'/api/rest';
	/* import { Request } */require_once $cwd.'_internal/RequestServer.php';
	$request= new Request($config, $get, $method, $body);
	extract($config->vars_shared);
	
	$folder= $cwd.$request->targetPath();
	$path= realpath($folder);
	if($path===false||!is_dir($path)) throw new \Exception("Endpoint '$folder' doesn’t exist.", 404);
	$file= $path.'/'.$request->method.'.php';
	if(file_exists($file)) return require_once $file;
}

php_rest_api's People

Contributors

jaandrle avatar

Watchers

 avatar

Forkers

jaandrle

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.