この質問はもはや最新ではありません-Googleは2012年に非公式の天気APIをシャットダウンしました
友達のウェブページに天気予報を載せたいのですが。私が
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>
しかし、「en」の代わりに「hr」を使用したため、上記のコードは機能しません(hr =クロアチア語):
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')
は動作する構文ですが、返されるデータは英語で、温度は華氏です。
間違ったUTF-8エンコーディングプロパティの問題だと思います。
正確なクロアチア語のテキストを取得して華氏を摂氏に変換する方法がわかりません。
その後、 F-to-Cソリューションへのリンクを見つけ、19行目を変更しました。
<?= $current[0]->temp_f['data'] ?>° F,に
<?= $current[0]->temp_c['data'] ?>° C,(APIが摂氏を処理しているように見えるため、現在のバージョンでは使用していません。)
言語を「en」に設定しながらCの度数を維持するには、を使用できます
en-gb。