<?php
//Путь к Вашему xml файлу
$xml_file="http://meteoinfo.ru/xml3/export/pay/1/1.xml";
//Подключение к БД, укажите адрес сервера, имя пользователя, пароль и название БД.
$host='site.ru';
$user='user';
$pasd='pass';
$db='forecast';
mysql_pconnect($host,$user,$pasd) or die ('Нет подключения к базе!!!');
mysql_select_db($db) or die ('Ошибка при подключении!!!');
//Копирование xml-файла к себе на хостинг
function http_get($url)
{
$buffer = "";
$url_stuff = parse_url($url);
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;
$fp = fsockopen($url_stuff['host'], $port);
$query = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
$query .= 'Host: ' . $url_stuff['host'];
$query .= "\n\n";
fwrite($fp, $query);
while ($tmp = fread($fp, 1024))
{
$buffer .= $tmp;
}
preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
return substr($buffer, - $parts[1]);
}
$fp = fopen ("forecast.xml", "w+");
fwrite ($fp, http_get($xml_file));
fclose ($fp);
//Парсинг xml-файла
$xml=domxml_open_file('/home/server.ru/www/forecast.xml');
//Пожалуйста указывайте абсолютный путь к файлу!
$root = $xml->document_element();
$root_child = $root->child_nodes();
for ($i = 0; $i < sizeof($root_child); $i++) {
if($root_child[$i]->node_name()=='town'){
$name=iconv("utf-8","windows-1251",$root_child[$i]->get_attribute('name'));
$ind=$root_child[$i]->get_attribute('id');
$country=iconv("utf-8","windows-1251",$root_child[$i]->get_attribute('country'));
$region=iconv("utf-8","windows-1251",$root_child[$i]->get_attribute('region'));
$town_child = $root_child[$i]->child_nodes();
for ($j = 0; $j < sizeof($town_child); $j++) {
if($town_child[$j]->node_name()=='date'){
$day=$town_child[$j]->get_attribute('day');
$date_child = $town_child[$j]->child_nodes();
for ($k = 0; $k < sizeof($date_child); $k++) {
switch ($date_child[$k]->node_name()) {
case 'tday':
$tday=$date_child[$k]->get_content();
break;
case 'tnight':
$tnight=$date_child[$k]->get_content();
break;
case 'prec':
$prec=$date_child[$k]->get_content();
break;
case 'prec_prob':
$prec_prob=$date_child[$k]->get_content();
break;
case 'wind_dir':
$wind_dir=$date_child[$k]->get_content();
break;
case 'windspeed':
$windspeed=$date_child[$k]->get_content();
break;
case 'weather_conditions':
$weather_conditions=iconv("utf-8","windows-1251",$date_child[$k]->get_content());
break;
case 'pday':
$pday=$date_child[$k]->get_content();
break;
case 'pnight':
$pnight=$date_child[$k]->get_content();
break;
}
}
//Запись иформации в БД
$res=mysql_query("insert into
forecast (Ind,Station,Country,Region,dat,
tday,tnight,prec,prec_prob,wind_dir,
wind_speed,
weather_conditions,pday,pnight) values (".
$ind.",\"".$name."\",\"".$country."\",\"".
$region."\",\"".$day."\",".$tday.",".
$tnight.",".$prec.",".$prec_prob.",".
$wind_dir.",".$windspeed.",\"".
$weather_conditions."\",".$pday.",".
$pnight.")");
}
}
}
}
?>
|