Изменение иконки погоды в API погоды Google - PullRequest
2 голосов
/ 10 февраля 2011

Привет, я просто хочу спросить, как лучше всего поменять иконку погоды в google weather api, изменив ее путь на mysite.com/images/weather вместо / ig / images / weather.Я видел такую ​​же проблему в переполнении стека, но я не знаю, как реализовать ее в своем коде.

вот мой код:

    <?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 } ?>

Большое спасибо заваша помощь

Ответы [ 3 ]

1 голос
/ 21 мая 2012

Возможно, используйте функцию php basename, чтобы извлечь имя файла иконки:

$iconData = '/my/new/path/' . basename( $current[0]->icon['data'] );
0 голосов
/ 13 марта 2011

Ну, в надежде, что это поможет кому-то еще, я нашел решение:

использование: str_replace !!

Вместо записи:

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';}

...

и т. Д.

...

для $forecast->icon['data'] и $current[0]->icon['data']

просто используйте:

$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

и

    <?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

...
    ?>

И, как теперь вы можете видеть, теперь используйте новые $iconData и $iconData2, например, например:

 <?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 } ?>
0 голосов
/ 10 февраля 2011

Я предлагаю вам использовать PHP-функцию preg_replace , чтобы заменить часть URL-адреса, имеющуюся в $forecast->icon.

...