• 0

[PHP] Google Weather API / xml help!


Question

I am attempting to add weather information to a page using the google weather API (of which documentation is scarce)

This is what I have so far.

HEAD

<?php
	$xml = simplexml_load_file('http://www.google.com/ig/api?weather=43055');
	$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");
?>

BODY

        <h2>Today's weather</h2>
        <div class="weather">		
            <img src="<?php= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"/>
            <span class="condition">
            <?php= $current[0]->temp_f['data'] ?>° F,
            <?php= $current[0]->condition['data'] ?>
            </span>
        </div>

However when I run it the XML does produce Data yet the page displays this

weather temp_f['data'] ?>? F, condition['data'] ?>

The word "weather" is actually an image that looks just like the text if i had not selected it to copy i would not have known it were an image.

Am i doing something wrong?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I don't think <?php=$a ?> is a valid PHP tag. You can use <?php echo $a; ?> or <?=$a ?> (if short_tags is enabled) but there's no such thing as <?php=$a ?>. (I like the idea though. :p)

Try replacing those faulty tags with valid alternatives and see if it works then.

Link to comment
Share on other sites

  • 0

Calculator is correct.

Here's the working code:

&lt;?php

function dump($var) {
	print('&lt;pre&gt;');
	print_r($var);
	print('&lt;/pre&gt;');
}

$xml 	= simplexml_load_file('http://www.google.com/ig/api?weather=43055');
$information = $xml-&gt;xpath("/xml_api_reply/weather/forecast_information");
$current = $xml-&gt;xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml-&gt;xpath("/xml_api_reply/weather/forecast_conditions");

?&gt;	


	&lt;h2&gt;Today's weather&lt;/h2&gt;
	&lt;div class="weather"&gt;           
		&lt;img src="&lt;?php echo 'http://www.google.com' . $current[0]-&gt;icon['data']?&gt;" alt="weather"/&gt;
		&lt;span class="condition"&gt;
		&lt;?php echo $current[0]-&gt;temp_f['data'] ?&gt;° F,
		&lt;?php echo $current[0]-&gt;condition['data'] ?&gt;
		&lt;/span&gt;
	&lt;/div&gt;


&lt;?php
	dump($information);
	dump($current);
	dump($forecast_list);
?&gt;

Here's a link to it working : http://psp83.co.uk/scripts/weather.php

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.