2

この質問はもはや最新ではありません-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'] ?>&deg; 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'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; 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エンコーディングプロパティの問題だと思います。

正確なクロアチア語のテキストを取得して華氏を摂氏に変換する方法がわかりません。


  1. その後、 F-to-Cソリューションへのリンクを見つけ、19行目を変更しました。

    <?= $current[0]->temp_f['data'] ?>&deg; F,

    <?= $current[0]->temp_c['data'] ?>&deg; C,

    (APIが摂氏を処理しているように見えるため、現在のバージョンでは使用していません。)

  2. 言語を「en」に設定しながらCの度数を維持するには、を使用できますen-gb

4

2 に答える 2

6

エンコーディングの問題:

何らかの理由で、Googleは適切なエンコーディング宣言なしでXMLコンテンツを返します。次のようなものが期待されます。

<?xml version='1.0' encoding='ISO-8859-2'?>

ただし、ヘッダーのencoding属性はスキップされます。これにより、simplexml_load_file関数はUTF-8のデフォルトのエンコーディングを想定します。XML仕様ではUTF-8がフォールバックのデフォルトエンコーディングとして定義されているため、これはAPI実装のバグと見なします。

これを補うために、次のようなことを試してください。

<?php
$URL = "http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr";
$dataInISO = file_get_contents($URL);
$dataInUTF = mb_convert_encoding($dataInISO, "UTF-8", "ISO-8859-2");
$xml = simplexml_load_string($dataInUTF);
...

これはうまくいくようです。ISO-8859-2の値は純粋な推測でした。

華氏/摂氏

このAPIで華氏ではなく摂氏で温度データを提供するように要求する簡単な方法がわかりません(公式のドキュメントが見つかりませんでした、私は盲目ですか?)。ただし、FからCへの変換はまったく難しいことではありません。

この式を試してください:

(°F  -  32)  x  5/9 = °C

あなたは何千もの場所で見つけることができます。http://www.manuelsweb.com/temp.htmから取得しました

于 2011-02-27T22:39:02.087 に答える
2

google xmlは摂氏で温度を返し、current_conditons内のtemp_cタグを探します

于 2011-04-27T21:38:57.757 に答える