Skip to content

Instantly share code, notes, and snippets.

@cisoun
Created June 24, 2025 20:39
Show Gist options
  • Save cisoun/f857f0658002c5b849c0b9ad53a30814 to your computer and use it in GitHub Desktop.
Save cisoun/f857f0658002c5b849c0b9ad53a30814 to your computer and use it in GitHub Desktop.
Mockup PHP API

Mockup PHP API

Serve a fake customer API as /api/customer/:id with Apache and some PHP.

/srv/http/api/customer/index.php:

<?php
// Simple customer array (simulate a database)
$customers = [
	1 => [
		'first_name' => 'Alice',
		'last_name' => 'Smith',
		'status' => 'active'
	],
	2 => [
		'first_name' => 'Bob',
		'last_name' => 'Johnson',
		'status' => 'inactive'
	]
];

// Get the request URI and method
$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];

// Basic routing logic
if ($requestMethod === 'GET' && preg_match('#^/api/customer/(\d+)$#', $requestUri, $matches)) {
	$customerId = (int)$matches[1];

	if (isset($customers[$customerId])) {
		header('Content-Type: application/json');
		echo json_encode($customers[$customerId]);
	} else {
		http_response_code(404);
		echo json_encode(['error' => 'Customer not found']);
	}
} else {
	http_response_code(400);
	echo json_encode(['error' => 'Invalid request']);
}

Redirect all requests in the same folder:

/srv/http/api/customer/.htaccess:

RewriteEngine On
RewriteRule ^ index.php [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment