-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
62 lines (51 loc) · 2.04 KB
/
Copy pathapi.php
File metadata and controls
62 lines (51 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// api.php - O Backend Seguro
header('Content-Type: application/json');
$TOMTOM_KEY = 'SUA API TOMTOM AQUI'; // Substitua pela sua chave de API TomTom
// Recebe qual ação o frontend quer (busca, rota ou chave publica)
$action = $_GET['action'] ?? '';
// Função auxiliar para fazer requisição cURL
function requestTomTom($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Retorna o status HTTP correto para o frontend
http_response_code($httpCode);
return $response;
}
switch ($action) {
case 'get_map_key':
// Retorna a chave APENAS para inicializar o mapa visual.
// Lembre-se: Configure restrição de DOMÍNIO no painel da TomTom.
echo json_encode(['key' => $TOMTOM_KEY]);
break;
case 'search':
// Proxy para busca de endereços
$query = urlencode($_GET['query'] ?? '');
if (strlen($query) < 3) { echo json_encode([]); exit; }
$url = "https://api.tomtom.com/search/2/search/{$query}.json?key={$TOMTOM_KEY}&limit=5&countrySet=BR&language=pt-BR";
echo requestTomTom($url);
break;
case 'route':
// Proxy para cálculo de rota
$start = $_GET['start'] ?? ''; // formato lat,lon
$end = $_GET['end'] ?? ''; // formato lat,lon
if (!$start || !$end) {
http_response_code(400);
echo json_encode(['error' => 'Origem e destino necessários']);
exit;
}
$locations = "{$start}:{$end}";
$url = "https://api.tomtom.com/routing/1/calculateRoute/{$locations}/json?key={$TOMTOM_KEY}&traffic=true&maxAlternatives=3&departAt=now&routeRepresentation=polyline";
echo requestTomTom($url);
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Ação inválida']);
break;
}
?>