2

Hi I just want to ask what is the bestway to change the weather icon on google weather api, changing it's path to mysite.com/images/weather rather than /ig/images/weather. I saw one with the same problem here in stack overflow but I don't know how to implement it on my code.

here is my code:

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

<h1>New-York City Weather</h1>
<div class="weather"> 
<img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather" />
<div class="condition"><strong>Today</strong><br />
<?php echo $current[0]->temp_f['data'] ?>° F,<?php echo $current[0]->condition['data'] ?>
</div>
</div>

<?php foreach ($forecast_list as $forecast) { ?>

    <div class="weather">
        <img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather" />
        <div class="condition">
            <strong><?php echo $forecast->day_of_week['data']; ?></strong><br />
                <?php echo $forecast->low['data'] ?>° F - <?php echo $forecast->high['data'] ?>° F,
            <?php echo $forecast->condition['data'] ?>
        </div>
    </div>
<?php } ?>

Thank you so much for your help

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Frank Sean
  • 21
  • 1
  • 2

3 Answers3

1

Perhaps use the php function basename to extract the name of the icon file:

$iconData = '/my/new/path/' . basename( $current[0]->icon['data'] );
Simon
  • 31,675
  • 9
  • 80
  • 92
dutch201
  • 11
  • 1
0

I suggest you tu use the PHP function preg_replace to replace part of the URL you have in $forecast->icon.

ChrisJ
  • 5,161
  • 25
  • 20
  • Hi thank you for your advice, unfortunately I have only limited knowledge in php Im more of a designer not a developer it you can elaborate more I would really appreciate it. thanks – Frank Sean Feb 10 '11 at 19:43
  • well... it's past about an year. I'll appreciate it too. Anyone? (we'd like a little help with changing default images!) – Roko C. Buljan Mar 04 '11 at 22:20
0

Well in the hope this will help someone else I found the solution:

use: str_replace !!

Instead of writing:

if($forecast->icon['data'] == '/ig/images/weather/sunny.gif') {$forecast->icon['data'] =            'path/to/folder/sunny.png';}
if($forecast->icon['data'] == '/ig/images/weather/mostly_sunny.gif') {$forecast->icon['data'] =     'path/to/folder/mostly_sunny.png';}
if($forecast->icon['data'] == '/ig/images/weather/partly_cloudy.gif') {$forecast->icon['data'] =    'path/to/folder/partly_cloudy.png';}
if($forecast->icon['data'] == '/ig/images/weather/mostly_cloudy.gif') {$forecast->icon['data'] =    'path/to/folder/mostly_cloudy.png';}
if($forecast->icon['data'] == '/ig/images/weather/chance_of_storm.gif') {$forecast->icon['data'] =  'path/to/folder/chance_of_storm.png';}

...

and so on

...

both for $forecast->icon['data'] and $current[0]->icon['data']

just use:

$iconData = str_replace("/ig/images/weather/", "path/to/folder/",  $current[0]->icon['data']);
$iconData = str_replace(".gif", ".png", $iconData); //if you use different image extension

and

    <?php foreach ($forecast_conditions as $forecast) : 
    // CUSTOM ICONS

$iconData2 = str_replace("/ig/images/weather/", "path/to/folder/",  $forecast->icon['data']);
$iconData2 = str_replace(".gif", ".png", $iconData2);//if you use different image extension

...
    ?>

And as now you can see from now on use the new $iconData and $iconData2 from now on like Ex:

 <?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=new-york');
$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");
//ADDED:
$iconData = str_replace("/ig/images/weather/", "path/to/file/",  $current[0]->icon['data']);
$iconData = str_replace(".gif", ".png", $iconData);
?> 
<h1>New-York City Weather</h1>
<div class="weather"> 
<img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather" />
<div class="condition"><strong>Today</strong><br />
<?php echo $current[0]->temp_f['data'] ?>° F,<?php echo $iconData ?>
</div>
</div>

<?php foreach ($forecast_list as $forecast) { 
//ADDED
$iconData2 = str_replace("/ig/images/weather/", "path/to/file/",  $forecast->icon['data']);
$iconData2 = str_replace(".gif", ".png", $iconData2);
?>



    <div class="weather">
        <img src="<?php echo 'http://www.google.com' . $iconData2 ?>" alt="weather" />
        <div class="condition">
            <strong><?php echo $forecast->day_of_week['data']; ?></strong><br />
                <?php echo $forecast->low['data'] ?>° F - <?php echo $forecast->high['data'] ?>° F,
            <?php echo $forecast->condition['data'] ?>
        </div>
    </div>
<?php } ?>
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313