Ошибка при SimpleXMLElement
Добрый вечер) Подскажите пожалуйста:
Что это за ошибка такая :
Fatal error: Uncaught Exception: String could not be parsed as XML in C:\xampp\htdocs\weather\index.php:124 Stack trace: #0 C:\xampp\htdocs\weather\index.php(124): SimpleXMLElement->__construct('https://www.yr....') #1 {main} thrown
И где я проморгала этот момент. Не подскажете как править мой код? Голову уже какое время сижу ломаю:/ код:
if (isset($_GET['city']))
{
$url = 'https://www.yr.no/place/'. $_GET['city'] .'/forecast.xml';
$data = new SimpleXMLElement(file_get_contents($url));
$city = $data->location->name;
$dir = 'xml/'.$city.'.xml';
// проверка, на то, что дата должна быть не в прошлом
if (strtotime(date('d.m.Y', $_GET['date'])) < strtotime(date('d.m.Y')))
{
echo " !!наша дата корректна";
// проверка, есть ли у нас уже такой файл?
if(file_exists($dir))
{
echo " !!у нас уже есть такой файл";
$src = file_get_contents($dir);
$data = new SimpleXMLElement($src);
// если файл есть, проверяем на актуальность
$lastupdate = $data->meta->lastupdate;
if(strtotime(date('d.m.Y', $lastupdate)) < strtotime(date('d.m.Y')))
{
echo " !!наш файл устарел, скачиваем и парсим";
// если файл устарел, скачиваем его и парсим
file_put_contents($dir, $url);
$src = file_get_contents($dir);
$data = new SimpleXMLElement($src);
$str = $data->forecast->tabular->time;
foreach ($data->forecast->tabular->time as $str)
{
if(strtotime($_GET["date"]) >= strtotime($str['from']) && strtotime($_GET['date']) <= strtotime($str['to']))
{
$forecast["Temperature:"] = (string) $str->temperature["value"];
$forecast["Precipitation:"] = (string) $str->precipitation["value"];
$windDirection = $str->windDirection["name"];
$forecast["Wind:"] = (string) $str->windSpeed["mps"] . " m/s from $windDirection";
$date = date ("d.m.Y H:i", strtotime($_GET['date']));
$city = $data->location->name;
$result = "OK";
break;
}
else {
$result = "ERROR";
$error_message = "Not found";
}
}
}
// если файл актуальный - парсим
else {
echo " !!наш файл актуальный, парсим";
$src = file_get_contents($dir);
$data = new SimpleXMLElement($src);
$str = $data->forecast->tabular->time;
foreach ($data->forecast->tabular->time as $str)
{
if(strtotime($_GET["date"]) >= strtotime($str['from']) && strtotime($_GET['date']) <= strtotime($str['to']))
{
$forecast["Temperature:"] = (string) $str->temperature["value"];
$forecast["Precipitation:"] = (string) $str->precipitation["value"];
$windDirection = $str->windDirection["name"];
$forecast["Wind:"] = (string) $str->windSpeed["mps"] . " m/s from $windDirection";
$date = date ("d.m.Y H:i", strtotime($_GET['date']));
$city = $data->location->name;
$result = "OK";
break;
}
else {
$result = "ERROR";
$error_message = "Not found";
}
}
}
}
// если у нас нет такого файла, скачиваем и парсим
else {
echo " !!у нас нет такого файла, скачиваем и парсим";
file_put_contents($dir, $url);
$src = file_get_contents($dir);
$data = new SimpleXMLElement($src);
$str = $data->forecast->tabular->time;
foreach ($data->forecast->tabular->time as $str)
{
if(strtotime($_GET["date"]) >= strtotime($str['from']) && strtotime($_GET['date']) <= strtotime($str['to']))
{
$forecast["Temperature:"] = (string) $str->temperature["value"];
$forecast["Precipitation:"] = (string) $str->precipitation["value"];
$windDirection = $str->windDirection["name"];
$forecast["Wind:"] = (string) $str->windSpeed["mps"] . " m/s from $windDirection";
$date = date ("d.m.Y H:i", strtotime($_GET['date']));
$city = $data->location->name;
$result = "OK";
break;
}
else {
$result = "ERROR";
$error_message = "Not found";
}
}
}
}
else {
$result = "ERROR";
$error_message = "Incorrect date or city";
}
}
else {
echo "else";
$result = "";}