Skip to content

Instantly share code, notes, and snippets.

@dexit
Created April 15, 2018 14:09
Show Gist options
  • Save dexit/ef6fab604b84fa3c527d0ca6141ef613 to your computer and use it in GitHub Desktop.
Save dexit/ef6fab604b84fa3c527d0ca6141ef613 to your computer and use it in GitHub Desktop.
Building a Simple Web API with Vanilla PHP

Building a Simple Web API with Vanilla PHP

This article is inspired by Brad Cypert's blog post: http://www.bradcypert.com/building-a-simple-api-in-php-using-lumen/.

Why should we build a API with Vanilla PHP? Because you can, and it makes fun.

Let's go

  • Create a directory /src

  • Create a file: src/functions.php

function router($httpMethods, $route, $callback, $exit = true)
{
    static $path = null;
    if ($path === null) {
        $path = parse_url($_SERVER['REQUEST_URI'])['path'];
        $scriptName = dirname(dirname($_SERVER['SCRIPT_NAME']));
        $scriptName = str_replace('\\', '/', $scriptName);
        $len = strlen($scriptName);
        if ($len > 0 && $scriptName !== '/') {
            $path = substr($path, $len);
        }
    }
    if (!in_array($_SERVER['REQUEST_METHOD'], (array) $httpMethods)) {
        return;
    }
    $matches = null;
    $regex = '/' . str_replace('/', '\/', $route) . '/';
    if (!preg_match_all($regex, $path, $matches)) {
        return;
    }
    if (empty($matches)) {
        $callback();
    } else {
        $params = array();
        foreach ($matches as $k => $v) {
            if (!is_numeric($k) && !isset($v[1])) {
                $params[$k] = $v[0];
            }
        }
        $callback($params);
    }
    if ($exit) {
        exit;
    }
}
  • Create a file .htaccess in the project root directory.
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
  • Create a directory /public
  • Create a file: public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
  • Create a file: public/index.php
<?php

require_once __DIR__ . '/../src/functions.php';

// Default index page
router('GET', '^/$', function() {
    echo '<a href="users">List users</a><br>';
});

// GET request to /users
router('GET', '^/users$', function() {
    echo '<a href="users/1000">Show user: 1000</a>';
});

// With named parameters
router('GET', '^/users/(?<id>\d+)$', function($params) {
    echo "You selected User-ID: ";
    var_dump($params);
});

// POST request to /users
router('POST', '^/users$', function() {
    header('Content-Type: application/json');
    $json = json_decode(file_get_contents('php://input'), true);
    echo json_encode(['result' => 1]);
});

header("HTTP/1.0 404 Not Found");
echo '404 Not Found';

Start the webserver

cd public
php -S localhost:8080

Open the browser: http://localhost:8080

Have fun :-)

@pablowako
Copy link

This is pretty cool! Love it!
How would you go about routing files? Since you're passing everything to index.php in your .htaccess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment