Script Meteo Php
Pagina 1 din 1
Script Meteo Php
- Spoiler:
- Cod:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Starea vremii</title>
</head>
<body>
<?php
/*
* generare informatii meteo
*
* Autor: Birkoff
* Contact: (ar fi reclama sa las)
* Data: 27.03.2010
*/
/*
* setarea fisierului xml de cache
*
* @param str $fisier_xml - calea si numele fisierului xml ce va fi folosit pentru cache
* @param str $localitate - numele localitatii pentru care se genereaza cache
*
* @return bool - true daca cache exista, false daca a dat eroare la citire/creare
*/
function cache_xml_meteo($fisier_xml, $localitate)
{
// cache se reface la fiecare ora
$diferenta_timp = 1*60*60*1;//1 ora
$expira = time()-$diferenta_timp;
// daca nu exista sau e prea vechi
if ( ! file_exists($fisier_xml) || filesize($fisier_xml) == 0 || filemtime($fisier_xml) < $expira )
{
// se preia
$continut = file_get_contents('http://www.google.com/ig/api?weather='.str_replace(" ","+",$localitate).'&hl=ro');
// daca nu sa putut prelua
if ( ! $continut )
return false;
// daca datele meteo sunt ok
if( stripos($continut,"este momentan")===false)
{
$continut=str_replace('<?xml version="1.0"?>', '<?xml version="1.0" encoding="ISO-8859-2"?>',$continut);
$continut=strtolower($continut);
// se salveaza cache
$is_cached = file_put_contents($fisier_xml, $continut);
// daca a dat eroare la salvare
if ( ! $is_cached )
return false;
// daca sa salvat ok
else
return true;
}
// daca datele meteo nu sunt ok
else
return false;
}
// daca exista deja si e creat in intervalul specificat
else
return true;
}
/*
* generarea datelor pentru afisare
*
* @param str $fisier_xml - calea si numele fisierului xml de unde se vor citi datele
* @param int $ce_afisam -
*
* 1 afiseaza doar temperatura, starea vremii, umiditate - fara imagine
* 2 afiseaza starea curenta (1) + imagine
* 3 afiseaza starea curenta (2) + prognoza
* 4 afiseaza doar prognoza
* 5 afiseaza tot (3) + harta orasului (google map)
*
* @return str $info
*/
function generate_info_meteo($fisier_xml, $ce_afisam=1)
{
// se seteaza variabila ce va contine informatiile meteo
$info = '';
// se verifica ce trebuie afisat
if ( $ce_afisam < 1 || $ce_afisam > 5 )
$ce_afisam = 1;
// se preia datele
$continut = file_get_contents($fisier_xml);
// daca a dat eroare
if ( ! $continut )
{
$info = 'Nu se poate citi fisierul xml cu datele meteo - '.$fisier_xml;
return $info;
}
// se parseaza datele
$xml = simplexml_load_string($continut);
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
// se paseaza datele la $info
switch ( $ce_afisam )
{
default:
case 1: // afiseaza doar temperatura, starea vremii, umiditate - fara imagine
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="center" valign="top"><b>Vremea in '.ucfirst($information[0]->city['data']).'</b></td>
</tr>
<tr>
<td align="center" valign="top">Temperatura: '.$current[0]->temp_c['data'].'°C</td>
</tr>
<tr>
<td align="center" valign="top">Starea vremii: '.$current[0]->condition['data'].'</td>
</tr>
<tr>
<td align="center" valign="top">'.ucfirst($current[0]->humidity['data']).'</td>
</tr>
</table>';
break;
case 2: // afiseaza starea curenta (1) + imagine
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2" align="center" valign="top"><strong>Vremea in '.ucfirst($information[0]->city['data']).'</strong></td>
</tr>
<tr>
<td rowspan="3" align="center" valign="middle">
<img src="http://www.google.com'.$current[0]->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top">Temperatura: '.$current[0]->temp_c['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top">Starea vremii: '.$current[0]->condition['data'].'</td>
</tr>
<tr>
<td align="left" valign="top">'.ucfirst($current[0]->humidity['data']).'</td>
</tr>
</table>';
break;
case 3: // afiseaza starea curenta (2) + prognoza
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2" align="center" valign="top">
<strong>Vremea in '.ucfirst($information[0]->city['data']).'</strong>
</td>
</tr>
<tr>
<td rowspan="3" align="center" valign="middle">
<img src="http://www.google.com'.$current[0]->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top">Temperatura: '.$current[0]->temp_c['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top">Starea vremii: '.$current[0]->condition['data'].'</td>
</tr>
<tr>
<td align="left" valign="top">'.ucfirst($current[0]->humidity['data']).'</td>
</tr>
</table>
';
// tabelele pentru prognoza
foreach ($forecast_list as $forecast)
{
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="center" valign="top"><strong>'.ucfirst($forecast->day_of_week['data']).'</strong></td>
<td align="left" valign="top"> Minima: '.$forecast->low['data'].'°C</td>
</tr>
<tr>
<td rowspan="2" align="center" valign="middle">
<img src="http://www.google.com'.$forecast->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top"> Maxima: '.$forecast->high['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top"> Prognoză: '.$forecast->condition['data'].'</td>
</tr>
</table>
';
}
break;
case 4: // afiseaza doar prognoza
// tabelele pentru prognoza
foreach ($forecast_list as $forecast)
{
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="center" valign="top"><strong>'.ucfirst($forecast->day_of_week['data']).'</strong></td>
<td align="left" valign="top"> Minima: '.$forecast->low['data'].'°C</td>
</tr>
<tr>
<td rowspan="2" align="center" valign="middle">
<img src="http://www.google.com'.$forecast->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top"> Maxima: '.$forecast->high['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top"> Prognoză: '.$forecast->condition['data'].'</td>
</tr>
</table>
';
}
break;
case 5: // afiseaza tot (3) + harta orasului (google map)
// cap tabel
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="left" valign="top">';
// punctul 3
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2" align="center" valign="top">
<strong>Vremea in '.ucfirst($information[0]->city['data']).'</strong>
</td>
</tr>
<tr>
<td rowspan="3" align="center" valign="middle">
<img src="http://www.google.com'.$current[0]->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top">Temperatura: '.$current[0]->temp_c['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top">Starea vremii: '.$current[0]->condition['data'].'</td>
</tr>
<tr>
<td align="left" valign="top">'.ucfirst($current[0]->humidity['data']).'</td>
</tr>
</table>
';
// tabelele pentru prognoza
foreach ($forecast_list as $forecast)
{
$info .= '<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="center" valign="top"><strong>'.ucfirst($forecast->day_of_week['data']).'</strong></td>
<td align="left" valign="top"> Minima: '.$forecast->low['data'].'°C</td>
</tr>
<tr>
<td rowspan="2" align="center" valign="middle">
<img src="http://www.google.com'.$forecast->icon['data'].'" alt="weather" width="40" height="40" border="0" />
</td>
<td align="left" valign="top"> Maxima: '.$forecast->high['data'].'°C</td>
</tr>
<tr>
<td align="left" valign="top"> Prognoză: '.$forecast->condition['data'].'</td>
</tr>
</table>
';
}
// inchidere coloana
$info .= '</td>
<td align="left" valign="top">';
// harta
$info .= "<img src='http://maps.google.com/maps/api/staticmap?center=".str_replace(" ","+",$information[0]->city['data']).
"&zoom=9&size=350x500&sensor=false' width='350' height='500' alt='map' border='0' />";
// inchidere tabel
$info .= '</td>
</tr>
</table>';
break;
}
// se returneaza informatiile
return $info;
}
// specificam localitatea
$localitate = 'Bucuresti';
// calea si numele fisierului pentru cache
$fisier_xml = 'bucuresti_meteo.xml';
// se genereaza cache daca nu este deja
$is_cache = cache_xml_meteo($fisier_xml, $localitate);
if ( ! $is_cache )
echo 'Nu e cache';
else
{
// se afiseaza datele meteo din cache
echo generate_info_meteo($fisier_xml, 5); // numarul poate fi intre 1 si 5
}
?>
</body>
</html>
Schimbati in script ceea ce aveti nevoie. scriptul nu este facut de mine! L-am gasit undeva si mi s-a parut foarte folositor.
Enjoy.
Pagina 1 din 1
Permisiunile acestui forum:
Nu puteti raspunde la subiectele acestui forum