provide basic rest api

This commit is contained in:
frederik
2025-08-17 14:21:04 +02:00
committed by Nachtzuster
parent cea5dbfd97
commit 5f4cb4f101
2 changed files with 50 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__ . '/scripts/common.php');
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$requestMethod = $_SERVER['REQUEST_METHOD'];
if ($requestMethod !== 'GET') {
sendResponse405();
}
if (preg_match('#^/api/v1/image/(\S+)$#', $requestUri, $matches)) {
if ($config["IMAGE_PROVIDER"] === 'FLICKR') {
$image_provider = new Flickr();
} else {
$image_provider = new Wikipedia();
}
$sci_name = urldecode($matches[1]);
debug_log($sci_name);
$result = $image_provider->get_image($sci_name);
if ($result == false) {
http_response_code(404);
echo "Error 404! No image found!";
} else {
http_response_code(200);
header('Content-Type: application/json');
echo json_encode([
"status" => "success",
"message" => "successfully image data from database",
"data" => $result
]);
}
} else {
http_response_code(404);
echo "Error 404! No route found!";
}
function sendResponse405() {
http_response_code(405);
echo json_encode(["message" => "Method Not Allowed"]);
}