Skip to content

A generic way to implement (REST) API for PHP ≥5.2

License

Notifications You must be signed in to change notification settings

IndigoMultimediaTeam/php_rest_api

Repository files navigation

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;
}

About

A generic way to implement (REST) API for PHP ≥5.2

Topics

Resources

License

Stars

Watchers

Forks

Languages