<?php
class CSV2API {
    public function convert($csvFile) {
        $data = [];
        if (($handle = fopen($csvFile, "r")) !== FALSE) {
            $headers = fgetcsv($handle);
            while (($row = fgetcsv($handle)) !== FALSE) {
                $data[] = array_combine($headers, $row);
            }
            fclose($handle);
        }
        return json_encode($data, JSON_PRETTY_PRINT);
    }
}

// CLI Usage: php csv2api.php input.csv
if (php_sapi_name() === "cli" && $argc > 1) {
    $tool = new CSV2API();
    echo $tool->convert($argv[1]);
}
?>