i tried this api file on my joomla website but its not working with me, can you solve this error ?

<?php
// api.php

// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Allow cross-origin requests (you may want to restrict this in production)
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// Include necessary Joomla files
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);

try {
    require_once JPATH_BASE . '/includes/defines.php';
    require_once JPATH_BASE . '/includes/framework.php';
} catch (Exception $e) {
    echo json_encode(['error' => 'Failed to load Joomla framework: ' . $e->getMessage()]);
    exit;
}

// Create the application
try {
    $app = JFactory::getApplication('site');
} catch (Exception $e) {
    echo json_encode(['error' => 'Failed to initialize Joomla application: ' . $e->getMessage()]);
    exit;
}

// Get the database connection
try {
    $db = JFactory::getDbo();
} catch (Exception $e) {
    echo json_encode(['error' => 'Failed to get database object: ' . $e->getMessage()]);
    exit;
}

// API Key (store this securely and don't expose it in your code)
define('API_KEY', 'your_api_key_here');

// Function to verify API key
function verifyApiKey() {
    $headers = getallheaders();
    if (isset($headers['Authorization'])) {
        $authHeader = $headers['Authorization'];
        $apiKey = str_replace('Bearer ', '', $authHeader);
        return $apiKey === API_KEY;
    }
    return false;
}

// Function to handle errors
function handleError($message, $code = 400) {
    http_response_code($code);
    echo json_encode(['error' => $message]);
    exit;
}

// Verify API key before processing any request
if (!verifyApiKey()) {
    handleError('Unauthorized access', 401);
}

// Get the request method
$method = $_SERVER['REQUEST_METHOD'];

try {
    // Handle different request methods
    switch ($method) {
        case 'GET':
            if (isset($_GET['id'])) {
                getItem($_GET['id']);
            } elseif (isset($_GET['search'])) {
                searchItems($_GET['search']);
            } else {
                getAllItems();
            }
            break;
        default:
            handleError('Method not allowed', 405);
    }
} catch (Exception $e) {
    handleError('Internal server error: ' . $e->getMessage(), 500);
}

function getAllItems() {
    global $db;
    try {
        $query = $db->getQuery(true);
        $query->select('id, name, description, date_start, price, address')
              ->from($db->quoteName('#__djcf_items'))
              ->where($db->quoteName('published') . ' = 1')
              ->order($db->quoteName('date_start') . ' DESC');
        $db->setQuery($query);
        $results = $db->loadAssocList();
        
        if ($results === null) {
            throw new Exception('Database query failed');
        }
        
        echo json_encode(['items' => $results]);
    } catch (Exception $e) {
        handleError('Database error: ' . $e->getMessage(), 500);
    }
}

function getItem($id) {
    global $db;
    try {
        $query = $db->getQuery(true);
        $query->select('*')
              ->from($db->quoteName('#__djcf_items'))
              ->where($db->quoteName('id') . ' = ' . $db->quote((int)$id))
              ->where($db->quoteName('published') . ' = 1');
        $db->setQuery($query);
        $result = $db->loadAssoc();
        
        if ($result === null) {
            throw new Exception('Database query failed');
        }
        
        if (empty($result)) {
            handleError('Item not found', 404);
        }
        
        echo json_encode(['item' => $result]);
    } catch (Exception $e) {
        handleError('Database error: ' . $e->getMessage(), 500);
    }
}

function searchItems($searchTerm) {
    global $db;
    try {
        $searchTerm = $db->escape('%' . $searchTerm . '%');
        $query = $db->getQuery(true);
        $query->select('id, name, description, date_start, price, address')
              ->from($db->quoteName('#__djcf_items'))
              ->where($db->quoteName('published') . ' = 1')
              ->where(
                  $db->quoteName('name') . ' LIKE ' . $db->quote($searchTerm) . ' OR ' .
                  $db->quoteName('description') . ' LIKE ' . $db->quote($searchTerm)
              )
              ->order($db->quoteName('date_start') . ' DESC');
        $db->setQuery($query);
        $results = $db->loadAssocList();
        
        if ($results === null) {
            throw new Exception('Database query failed');
        }
        
        echo json_encode(['items' => $results]);
    } catch (Exception $e) {
        handleError('Database error: ' . $e->getMessage(), 500);
    }
}