繁体   English   中英

Google Weather API上的更改天气图标

[英]Changing weather icon on google weather api

嗨,我只是想问问更改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 } ?>

非常感谢你的帮助

也许使用php函数basename提取图标文件的名称:

$iconData = '/my/new/path/' . basename( $current[0]->icon['data'] );

我建议您使用PHP函数preg_replace替换$forecast->icon具有的一部分URL。

希望这对我找到解决方案的其他人有所帮助:

使用: 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

...
    ?>

从现在起您可以看到,像Ex一样从现在开始使用新的$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 } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM