<?php
// Simple PHP script to handle GET /api/products
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

// Only respond to GET requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Sample product data
    $products = [
        ['id' => 1, 'name' => 'Computer', 'price' => 1800.00],
        ['id' => 2, 'name' => 'Screen', 'price' => 350.00],
        ['id' => 3, 'name' => 'Phone', 'price' => 1250.00],
        ['id' => 4, 'name' => 'Book', 'price' => 14.99]
        // ... more products
    ];

    // Set content-type to JSON
    header('Content-Type: application/json');

    // Output the JSON-encoded products
    echo json_encode($products);
    exit;
}

// If endpoint doesn't match, return 404
http_response_code(404);
echo json_encode(['message' => 'Not Found']);


<?php
// Allow CORS
header("Access-Control-Allow-Origin: *"); // Or use "*" for public
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");

// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

// Handle POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $json = file_get_contents('php://input');
    $newProduct = json_decode($json, true);

    // Simulate saving product...

    http_response_code(201);
    echo json_encode([
        'message' => 'Products have been successfully sent to the server',
        'product' => $newProduct
    ]);
    exit();
}

// Handle unsupported methods
http_response_code(405);
echo json_encode(['error' => 'Only POST and OPTIONS allowed']);