A live bird collage card for your Home Assistant dashboard.
provide basic rest api
frederik5f4cb4f1012a
diff --git a/homepage/index.php b/homepage/index.phpindex cff395a9..3f24eda3 100644--- a/homepage/index.php+++ b/homepage/index.php@@ -1,5 +1,12 @@<?php+$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);++if (strpos($requestUri, '/api/v1/') === 0) {+ include_once 'scripts/api.php';+ die();+}+/* Prevent XSS input */$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);diff --git a/scripts/api.php b/scripts/api.phpnew file mode 100644index 00000000..cfd8f741--- /dev/null+++ b/scripts/api.php@@ -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"]);+}