38975-vm/api/place-details.php
2026-03-04 16:24:17 +00:00

68 lines
2.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/../includes/db.php';
header('Content-Type: application/json');
$place_id = $_GET['place_id'] ?? '';
$name = $_GET['name'] ?? '';
$category = $_GET['category'] ?? '';
if (empty($place_id)) {
echo json_encode(['error' => 'place_id is required']);
exit;
}
// 1. Check Cache
$stmt = $pdo->prepare("SELECT details FROM place_details_cache WHERE place_id = ?");
$stmt->execute([$place_id]);
$cached = $stmt->fetchColumn();
if ($cached) {
echo json_encode(json_decode($cached));
exit;
}
// 2. Google Places Details API
$apiKey = getenv('GOOGLE_MAPS_API_KEY');
$url = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$place_id&fields=name,rating,user_ratings_total,reviews,opening_hours,editorial_summary&language=tr&key=$apiKey";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (!isset($data['result'])) {
echo json_encode(['error' => 'Failed to fetch details']);
exit;
}
$result = $data['result'];
// 3. Generate Content (Placeholder / Logic for AI)
// In a real production app, call OpenAI API here if configured.
// Using static fallback as requested for now.
$aiContent = [
'why_visit' => ["Kapadokya'nın eşsiz güzelliklerini keşfedin", "Tarihi dokusuyla büyülenin", "Unutulmaz fotoğraflar çekin"],
'tips' => ["Sabah erken saatlerde ziyaret etmenizi öneririz.", "Rahat ayakkabılar giyin.", "Güneş kreminizi yanınıza alın.", "Yerel rehberlerden bilgi alabilirsiniz."],
'summary' => "{$result['name']}, bölgedeki en ilgi çekici ve kültürel açıdan zengin noktalardan biridir. Ziyaretçilerine hem doğal güzellikler hem de tarihi deneyimler sunmaktadır."
];
$finalResponse = [
'place_id' => $place_id,
'name' => $result['name'] ?? $name,
'summary' => $aiContent['summary'],
'rating' => $result['rating'] ?? 0,
'total_ratings' => $result['user_ratings_total'] ?? 0,
'is_open_now' => $result['opening_hours']['open_now'] ?? null,
'opening_hours' => $result['opening_hours']['weekday_text'] ?? [],
'why_visit' => $aiContent['why_visit'],
'tips' => $aiContent['tips'],
'reviews' => $result['reviews'] ?? []
];
// 5. Cache
$stmt = $pdo->prepare("INSERT IGNORE INTO place_details_cache (place_id, details) VALUES (?, ?)");
$stmt->execute([$place_id, json_encode($finalResponse)]);
// 6. Respond
echo json_encode($finalResponse);