2

こんにちは、Google 天気 API の天気アイコンを変更する最良の方法は何かをお聞きしたいのですが、パスを /ig/images/weather ではなく mysite.com/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 } ?>

手伝ってくれてどうもありがとう

4

3 に答える 3

1

おそらく php 関数basenameを使用して、アイコン ファイルの名前を抽出します。

$iconData = '/my/new/path/' . basename( $current[0]->icon['data'] );
于 2012-05-21T14:18:46.043 に答える
0

これが他の誰かに役立つことを願って、私は解決策を見つけました:

使用: 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 } ?>
于 2011-03-12T23:39:19.440 に答える
0

にある URL の一部を置き換えるには、PHP 関数preg_replace$forecast->iconを使用することをお勧めします。

于 2011-02-10T19:16:12.880 に答える