Merhaba Arkadaşlar,
Bugün sizlerle talep üzerine yazmış olduğum
Namaz Vakitleri API’ını paylaşmak istiyorum. Bir süre araştırma yaptıktan sonra, internetteki çoğu kaynağın ücretli API hizmeti sunduğunu ve maalesef ücretsiz bir çözüm bulmanın oldukça zor olduğunu fark ettim. Hatta bazı API’lar, çok sınırlı veri sunarak kullanıcıları mecburen ücretli sürüme geçmeye zorlayabiliyor.
Ancak, bu sorunu ortadan kaldırmak amacıyla, Türkiye’deki şehirlerin namaz vakitlerini kolayca sorgulayabileceğiniz
ücretsiz bir Namaz Vakitleri API'ı geliştirdim. Bu API ile, dilediğiniz şehir için namaz vakitlerini rahatça alabilirsiniz. Amacım, herkesin bu hizmetten faydalanabilmesidir. Veriler diyanetin sitesinden çekilmekte.
KOD: <?php
header('Content-Type: application/json; charset=utf-8');
class HttpClient
{
public function get(string $url): string
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
return $response ?: '';
}
}
class PrayerTimesFetcher
{
private HttpClient $client;
private string $baseUrl = "https://namazvakitleri.diyanet.gov.tr";
public function __construct(HttpClient $client)
{
$this->client = $client;
}
public function fetchPrayerTimes(int $cityId): ?array
{
$html = $this->client->get("{$this->baseUrl}/tr-TR/{$cityId}");
return $this->parsePrayerTimes($html);
}
private function parsePrayerTimes(string $html): ?array
{
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$table = $xpath->query('//*[@id="tab-1"]/div/table')->item(0);
if (!$table) {
return null;
}
$prayerTimes = [];
$rows = $table->getElementsByTagName('tr');
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
if ($cells->length >= 8) {
$prayerTimes[] = [
"miladi_tarih" => trim($cells->item(0)->textContent),
"hicri_tarih" => trim($cells->item(1)->textContent),
"imsak" => trim($cells->item(2)->textContent),
"gunes" => trim($cells->item(3)->textContent),
"ogle" => trim($cells->item(4)->textContent),
"ikindi" => trim($cells->item(5)->textContent),
"aksam" => trim($cells->item(6)->textContent),
"yatsi" => trim($cells->item(7)->textContent)
];
}
}
return $prayerTimes;
}
}
function normalizeString(string $string): string {
$search = ['ç', 'ğ', 'ı', 'i', 'ö', 'ş', 'ü'];
$replace = ['c', 'g', 'i', 'i', 'o', 's', 'u'];
return strtoupper(str_replace($search, $replace, trim($string)));
}
try {
$city = filter_input(INPUT_GET, 'city', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (!$city) {
throw new Exception("City parameter is required.");
}
$cities = [
9146 => "ADANA", 9158 => "ADIYAMAN", 9167 => "AFYONKARAHISAR", 9185 => "AĞRI",
9193 => "AKSARAY", 9198 => "AMASYA", 9206 => "ANKARA", 9225 => "ANTALYA",
9238 => "ARDAHAN", 9246 => "ARTVIN", 9252 => "AYDIN", 9270 => "BALIKESIR",
9285 => "BARTIN", 9288 => "BATMAN", 9295 => "BAYBURT", 9297 => "BILECIK",
9303 => "BINGOL", 9311 => "BITLIS", 9315 => "BOLU", 9327 => "BURDUR",
9335 => "BURSA", 9352 => "CANAKKALE", 9359 => "CANKIRI", 9370 => "CORUM",
9392 => "DENIZLI", 9402 => "DIYARBAKIR", 9414 => "DUZCE", 9419 => "EDIRNE",
9432 => "ELAZIG", 9440 => "ERZINCAN", 9451 => "ERZURUM", 9470 => "ESKISEHIR",
9479 => "GAZIANTEP", 9494 => "GIRESUN", 9501 => "GUMUSHANE", 9507 => "HAKKARI",
20089 => "HATAY", 9522 => "IĞDIR", 9528 => "ISPARTA", 9541 => "ISTANBUL",
9560 => "IZMIR", 9577 => "KAHRAMANMARAS", 9581 => "KARABUK", 9587 => "KARAMAN",
9594 => "KARS", 9609 => "KASTAMONU", 9620 => "KAYSERI", 9629 => "KILIS",
9635 => "KIRIKKALE", 9638 => "KIRKLARELI", 9646 => "KIRSEHIR", 9654 => "KOCAELI",
9676 => "KONYA", 9689 => "KUTAHYA", 9703 => "MALATYA", 9716 => "MANISA",
9726 => "MARDIN", 9737 => "MERSIN", 9747 => "MUGLA", 9755 => "MUS",
9760 => "NEVSEHIR", 9766 => "NIGDE", 9782 => "ORDU", 9788 => "OSMANIYE",
9799 => "RIZE", 9807 => "SAKARYA", 9819 => "SAMSUN", 9831 => "SANLIURFA",
9839 => "SIIRT", 9847 => "SINOP", 9854 => "SIRNAK", 9868 => "SIVAS",
9879 => "TEKIRDAG", 9887 => "TOKAT", 9905 => "TRABZON", 9914 => "TUNCELI",
9919 => "USAK", 9930 => "VAN", 9935 => "YALOVA", 9949 => "YOZGAT", 9955 => "ZONGULDAK"
];
$cityId = array_search(normalizeString($city), $cities, true);
if (!$cityId) {
throw new Exception("City not found.");
}
$client = new HttpClient();
$fetcher = new PrayerTimesFetcher($client);
$prayerTimes = $fetcher->fetchPrayerTimes($cityId);
if (!$prayerTimes) {
throw new Exception("Could not retrieve prayer times.");
}
echo json_encode(['status' => true, 'data' => $prayerTimes], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
echo json_encode(['status' => false, 'error' => $e->getMessage()], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}API ENDPOINTI:
/index.php?city=Ankara