PHP e XML-Google Weather API语言选择问题


PHP e XML — Google Weather API problem with language selection

我想将xml中的天气信息显示到此URL:http://www.google.com/ig/api?weather=roma&hl=使用这个脚本:

<?
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=roma&hl=it');
$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");
?>
<html>
    <head>
        <title>Google Weather API</title>
    </head>
    <body>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</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>Forecast</h2>
        <? 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>
        <? endforeach ?>
    </body>
</html>

但是脚本没有显示任何输出,如果我从simplexml_load_file的参数中删除语言规范(&hl=it),它就可以工作了。

Error_log.php输出:

[24-Jun-2011 11:12:53] PHP Warning:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: http://www.google.com/ig/api?weather=roma&amp;hl=it:1: parser error : Input is not proper UTF-8, indicate encoding !
Bytes: 0xE0 0x3A 0x20 0x35 in /home/site/public_html/test.php on line 2
[24-Jun-2011 11:12:53] PHP Warning:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: ialmente nuvoloso&quot;/&gt;&lt;temp_f data=&quot;81&quot;/&gt;&lt;temp_c data=&quot;27&quot;/&gt;&lt;humidity data=&quot;Umidit in /home/site/public_html/test.php on line 2
[24-Jun-2011 11:12:53] PHP Warning:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]:^ in /home/site/public_html/test.php on line 2
[24-Jun-2011 11:12:53] PHP Fatal error:  Call to a member function xpath() on a non-object in /home/site/public_html/test.php on line 3

使用此代码是因为外部xml不会从您的站点读取,所以请使用此curl代码

<?php
/*$xml = simplexml_load_file('http://www.google.com/ig/api?weather=islamabad');
$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");*/
$placename = 'islamabad'; // city where you want local weather
$place=urlencode($placename);
$place = utf8_encode($place);
$url = 'http://www.google.com/ig/api?weather='.$place;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
$raw_data = curl_exec ($ch);
curl_close ($ch);
$xml = simplexml_load_string($raw_data);
echo $condition = $xml->weather->current_conditions->condition['data'];
echo $temp_f = $xml->weather->current_conditions->temp_f['data'];
echo $humidity = $xml->weather->current_conditions->humidity['data'];
echo $icon = $xml->weather->current_conditions->icon['data'];
echo $wind_condition = $xml->weather->current_conditions->wind_condition['data'];
?>

我解决了添加以下行的问题:

 $url = file_get_contents('http://www.google.com/ig/api?weather=roma&hl=it');
 $xml = iconv("iso-8859-1", "utf-8", $url);
 $xml = simplexml_load_string($xml);