4 changed files with 148 additions and 0 deletions
@ -0,0 +1,51 @@ |
|||||
|
<?php |
||||
|
session_start(); |
||||
|
require "init.php"; |
||||
|
|
||||
|
// Kontrola: příjmeme jenom POST požadavky (teda informace z formuláře) |
||||
|
if($_SERVER["REQUEST_METHOD"] !== "POST") { |
||||
|
header("Location: index.php"); |
||||
|
exit; |
||||
|
} |
||||
|
|
||||
|
//ZÍSKÁNÍ dat z formuláře |
||||
|
$mesto = trim($_POST["mesto"] ?? ""); |
||||
|
//VALIDACE získáných dat |
||||
|
if ($mesto === ""){ |
||||
|
$_SESSION["chyba"] = "Zadejte název města"; |
||||
|
header("Location: index.php"); |
||||
|
exit; |
||||
|
} |
||||
|
// SESTAVENÍ URL a volání API |
||||
|
$url = $URL_BASE . "?" . http_build_query([ |
||||
|
"q" => $mesto, |
||||
|
"units" => "metric", |
||||
|
"lang" => "cz", |
||||
|
"appid" => $API_KEY |
||||
|
]); |
||||
|
//echo $url; |
||||
|
// file_get_contents stáhne obsah URL jako string |
||||
|
// @ potlačí varování (při chybě vrátí false) |
||||
|
$odpoved = @file_get_contents($url); |
||||
|
if ($odpoved === false) { |
||||
|
$_SESSION["chyba"] = "Nepodařilo se s pojit s API. Zkontrolujte název města"; |
||||
|
$_SESSION["mesto"] = $mesto; |
||||
|
header("Location: index.php"); |
||||
|
exit; |
||||
|
} |
||||
|
//echo $odpoved; |
||||
|
$data = json_decode($odpoved,true); |
||||
|
//echo $data; |
||||
|
|
||||
|
// kontrola chybového kodu |
||||
|
if (isset($data["cod"]) && $data["cod"] != 200){ |
||||
|
$_SESSION["chyba"] = "Město '$mesto' nebylo nalezeno"; |
||||
|
$_SESSION["mesto"] = $mesto; |
||||
|
header("Location: index.php"); |
||||
|
exit; |
||||
|
} |
||||
|
// ÚSPĚCH |
||||
|
$_SESSION["pocasi"] = $data; |
||||
|
header("Location: vysledek.php"); |
||||
|
exit; |
||||
|
?> |
||||
@ -0,0 +1,34 @@ |
|||||
|
<?php |
||||
|
session_start(); |
||||
|
//načteme chybu POKUD EXISTUJE |
||||
|
$chyba = $_SESSION["chyba"] ?? ""; |
||||
|
$mesto = $_SESSION["mesto"] ?? ""; |
||||
|
//Vyčistíme globální $_SESSION proměnné |
||||
|
unset($_SESSION["chyba"]); |
||||
|
unset($_SESSION["mesto"]); |
||||
|
?> |
||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>Document</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>OpenWeatherMap</h1> |
||||
|
<form action="actions.php" method="POST"> |
||||
|
<input |
||||
|
type="text" |
||||
|
name="mesto" |
||||
|
placeholder="Zadej město (např. Štětí,Praha)" |
||||
|
value="<?= htmlspecialchars($mesto)?>" |
||||
|
autofocus |
||||
|
> |
||||
|
<button type="submit">Hledat počasí pro město</button> |
||||
|
</form> |
||||
|
<!--Zobrazení chyby pokud nějaká exisstuje --> |
||||
|
<?php if ($chyba): ?> |
||||
|
<div><?= htmlspecialchars($chyba) ?></div> |
||||
|
<?php endif; ?> |
||||
|
</body> |
||||
|
</html> |
||||
@ -0,0 +1,6 @@ |
|||||
|
<?php |
||||
|
// odkomentovat v php.ini pro komunikaci v https -> extension=openssl |
||||
|
$API_KEY = "cdcbb82b14e8e3e85117686c68e19a5b"; |
||||
|
$URL_BASE = "https://api.openweathermap.org/data/2.5/weather" |
||||
|
|
||||
|
?> |
||||
@ -0,0 +1,57 @@ |
|||||
|
<?php |
||||
|
session_start(); |
||||
|
|
||||
|
if(!isset($_SESSION["pocasi"])){ |
||||
|
header("Location: index.php"); |
||||
|
exit; |
||||
|
} |
||||
|
|
||||
|
$pocasi = $_SESSION["pocasi"]; |
||||
|
//vyčtíme $_SESSION pamět po načtení do proměnné |
||||
|
unset($_SESSION["pocasi"]); |
||||
|
function formatCas($timestamp, $timezone){ |
||||
|
return date("H:i", $timestamp + $timezone); |
||||
|
} |
||||
|
?> |
||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>Počasí - <?php echo(htmlspecialchars($pocasi["name"]));?></title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>Počasí</h1> |
||||
|
<h3> |
||||
|
<?php echo(htmlspecialchars($pocasi["name"]));?> - |
||||
|
<?= htmlspecialchars($pocasi["sys"]["country"])?> |
||||
|
</h3> |
||||
|
<div> |
||||
|
<?= htmlspecialchars($pocasi["weather"][0]["description"])?> |
||||
|
</div> |
||||
|
<div> |
||||
|
Teplota - <?= htmlspecialchars($pocasi["main"]["temp"])?>°C |
||||
|
</div> |
||||
|
<div> |
||||
|
Pocitová - <?= htmlspecialchars($pocasi["main"]["feels_like"])?>°C |
||||
|
</div> |
||||
|
<div> |
||||
|
Vlhkost - <?= htmlspecialchars($pocasi["main"]["humidity"])?>% |
||||
|
</div> |
||||
|
<div> |
||||
|
Vítr - <?= htmlspecialchars($pocasi["wind"]["speed"]*3.6)?> km/h |
||||
|
</div> |
||||
|
<div> |
||||
|
Tlak - <?= htmlspecialchars($pocasi["main"]["pressure"])?> hPa |
||||
|
</div> |
||||
|
<div> |
||||
|
Východ slunce - <?= formatCas($pocasi["sys"]["sunrise"],$pocasi["timezone"])?> |
||||
|
</div> |
||||
|
<div> |
||||
|
Západ slunce - <?= formatCas($pocasi["sys"]["sunset"],$pocasi["timezone"])?> |
||||
|
</div> |
||||
|
|
||||
|
<br> |
||||
|
<a href="index.php"><button>ZPĚT</button></a> |
||||
|
</body> |
||||
|
</html> |
||||
Loading…
Reference in new issue