Skip to content
A super-simple PHP library for creating nice APIs
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
LICENSE.txt
README.md
composer.json

README.md

DynamicEndpoints

A super-simple PHP library for creating nice APIs. It allows you to have urls like api.foo.com/my/endpoint/2

The Idea

When writing an HTTP API in PHP it is much nicer to have a url like this: api.foo.com/books/2 instead of this api.foo.com/books.php?id=2. This is simple enough to do, use a custom apache 404 page, then parse the URL, but that can be a pain. So I made a tiny library for it.

Usage

For a working example, see the test directory. ###.htaccess In the root folder you have to have a .htaccess file that defines a main page, perhaps index.php as the 404 ErrorDoucument. It could look like this:

ErrorDocument 404 /dynamic-endpoints/test/index.php

PHP

First, import the sources:

require_once(__DIR__ . "/../src/DynamicEndpoint.php");

or use composer and just require your autoload.php:

composer require vogon101/dynamic-endpoints

Next, create an API object. $base is the path that you want all the APIs to come off of. For example for something like this: http://foo.com/my-thing/api/ the $base would be /my-thing/api. Base can be blank, for something at the root url.

$base = "/dynamic-endpoints/test";
$API = new BluePost\API($base);

Now, you have to register the endpoints. These define what URLs can be accessed through the API. The keys are the endpoint strings and the values are the files to be run if the endpoint is accessed. They can contain named variables and wildcards:

//Register the endpoints
//Variables are defined with %varName
//A .. is a wildcard
$API->register(Array(
    "/api/movie/../%name/%prop/.." => __DIR__ . "/movie.php",
    "/api/movie/" => __DIR__ . "/movie2.php",
    "/api/book/%id" => __DIR__ . "/book.php",
    "/api/book/name/%name" => __DIR__ . "/book2.php"
));

So /api/book/%id will match this /api/book/2 and it will pass $id with value 2. However because /api/book/name/%name is very similar to the %id one, if a user were to navigate to /api/book/name/ then the algorithm will match it to the %id endpoint, loading it with $id = "name"

Now we simply run the API. This will include the file specified if the enpoint matches. The variables will be set as specified in the scope of the file. If no endpoint is found, and array will be returned with an key of "Error".

//Look for an endpoint, will run the file if one is found
$result = $API->runEndpoint();
if (array_key_exists("error", $result)) var_dump ($result);

License

The software is avaliable under the Creative Commons Attribution 4.0 International. It can be found here: http://creativecommons.org/licenses/by/4.0/

You can’t perform that action at this time.