开发者

PHP e XML — Google Weather API problem with language selection

开发者 https://www.devze.com 2023-03-15 12:11 出处:网络
I want show weather information from xml to this URL: http://www.google.com/ig/api?weather=roma&hl=it using this script:

I want show weather information from xml to this URL: http://www.google.com/ig/api?weather=roma&hl=it using this script:

<?
$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]-&g开发者_开发百科t;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>

But the script does not show any output, if I delete the language specification (&hl=it) from simplexml_load_file's parameters, it works.

Error_log_php Output:

[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


user this code because external xml dont read from your site so use this curl code

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


I solved the problem adding these lines:

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

精彩评论

暂无评论...
验证码 换一张
取 消