Этот вопрос больше не актуален - Google закрыл неофициальный погодный API в 2012 году
Я бы хотел добавить прогноз погоды на страницу друга.
Когда я обращаюсь за
http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr
Браузер возвращает нужный контент, который я хотел бы проанализировать в PHP с помощью этого кода:
<?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr');
$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");
?>
<!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>VREMENSKA PROGNOZA</title>
</head>
<body>
<h1><?= print $information[0]->city['data']; ?></h1>
<h2>Danas</h2>
<div class="weather">
<img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
<span class="condition">
<?= $current[0]->temp_f['data'] ?>° F,
<?= $current[0]->condition['data'] ?>
</span>
</div>
<h2>Prognoza</h2>
<?php foreach ($forecast_list as $forecast) : ?>
<div class="weather">
<img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
<div><?= $forecast->day_of_week['data']; ?></div>
<span class="condition">
<?= $forecast->low['data'] ?>° F - <?= $forecast->high['data'] ?>° F,
<?= $forecast->condition['data'] ?>
</span>
</div>
<?php endforeach ?>
</body>
</html>
Но приведенный выше код не будет работать, потому что я использовал «hr» вместо «en» (hr = хорватский язык):
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')
- это рабочий синтаксис, но возвращенные данные на английском языке, а температура в градусах Фаренгейта.
Полагаю, дело в неправильном свойстве кодировки UTF-8.
Я не знаю, как получить точный хорватский текст и перевести градусы F в градусы Цельсия.
Впоследствии я нашел ссылку на F-to-C решение и изменил строку 19:
<?= $current[0]->temp_f['data'] ?>° F,
до
<?= $current[0]->temp_c['data'] ?>° C,
(я не использую его в текущей версии, потому что кажется, что API обрабатывает Цельсий.)
Чтобы сохранить градусы в Си, в то время как язык установлен на «en», вы можете использовать en-gb
.