56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|
namespace App;
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . '/rss/php/autoload.php');
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
use Exception;
|
|
|
|
class Main extends Sys{
|
|
|
|
|
|
public function __construct(){
|
|
|
|
}
|
|
|
|
public function getCountryData() {
|
|
$csvPath = $_SERVER['DOCUMENT_ROOT'] . '/rss/csv/worldcities.csv';
|
|
if (!file_exists($csvPath)) {
|
|
return ['status' => 'error', 'message' => 'CSV file not found'];
|
|
}
|
|
|
|
$handle = fopen($csvPath, 'r');
|
|
if (!$handle) {
|
|
return ['status' => 'error', 'message' => 'Failed to open CSV'];
|
|
}
|
|
|
|
$header = fgetcsv($handle);
|
|
$countryData = [];
|
|
|
|
while (($row = fgetcsv($handle)) !== false) {
|
|
$rowData = array_combine($header, $row);
|
|
|
|
$country = $rowData['country'] ?? null;
|
|
$city = $rowData['city'] ?? null;
|
|
$state = $rowData['admin_name'] ?? null;
|
|
|
|
if (!$country || !$city) {
|
|
continue;
|
|
}
|
|
|
|
$cityObj = ['city' => $city];
|
|
if ($state && $state !== $city) {
|
|
$cityObj['state'] = $state;
|
|
}
|
|
|
|
if (!isset($countryData[$country])) {
|
|
$countryData[$country] = [];
|
|
}
|
|
|
|
$countryData[$country][] = $cityObj;
|
|
}
|
|
|
|
fclose($handle);
|
|
return $this->createResponse('success', $countryData);
|
|
}
|
|
} |