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

时间:2011-06-23 17:12:37

标签: php

我希望使用此脚本显示xml到此URL的天气信息:http://www.google.com/ig/api?weather=roma&hl=it

<?
$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的参数中删除了语言规范(&amp; 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

2 个答案:

答案 0 :(得分:1)

我解决了添加这些行的问题:

 $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);

答案 1 :(得分:1)

使用此代码,因为外部xml不会从您的站点读取,因此请使用此卷曲代码

<?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'];
?>