dongluo1853 2013-03-12 08:09
浏览 69
已采纳

如何修改我的页面上的雅虎天气外观?

I've just got the basic yahoo weather feed on my page...

The following code renders the image below:

$ipaddress = $_SERVER['REMOTE_ADDR'];

// API username and key masked as sensitive and irrelevant to this question
$locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddress."&format=xml";

$xml = simplexml_load_file($locationstr);

$city = $xml->city;

// We'll only be accounting for certain cities to start off with...
switch ($city)
{
    case "Pretoria":
        $loccode = "SFXX0044";

        $weatherfeed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
        if (!$weatherfeed) die("weather check failed, check feed URL");
        $weather = simplexml_load_string($weatherfeed);

        readWeather($loccode);
        break;
}

function readWeather($loccode)
{
    $doc = new DOMDocument();
    $doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");

    $channel = $doc->getElementsByTagName("channel");

    foreach($channel as $ch)
    {
        $item = $ch->getElementsByTagName("item");

        foreach($item as $rcvd)
        {
            $desc = $rcvd->getElementsByTagName("description");

            // Save the weather data to a session variable for placement on the page
            $_SESSION["weather"] = $desc->item(0)->nodeValue;
        }
    }
}

My Yahoo Weather data

It looks pretty terrible in my opinion, so I want to change the overall design here to fit it in with the rest of my site.

I had an idea about rewriting the rendered html with jquery, but I'm not getting anywhere there.

Here's the code I'm getting rendered currently:

<div id="weather-feed">
    <img src="http://l.yimg.com/a/i/us/we/52/34.gif"/>
    <b>Current Conditions:</b>
    Partly Cloudy, 17 C
    <b>Forecast:</b>
    Tue - PM Thunderstorms. High: 26 Low: 16
    Wed - Mostly Sunny. High: 27 Low: 16

    <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Pretoria__SF/*http://weather.yahoo.com/forecast/SFXX0044_c.html">Full Forecast at Yahoo! Weather</a>
    (provided by <a href="http://www.weather.com" >The Weather Channel</a>)
</div>

Basically, all I want to do with this is to modify this rendered code as follows:

<div id="weather-feed">
    <div class="weather-icon">
        <img src="http://l.yimg.com/a/i/us/we/52/34.gif"/>
    </div>
    <div class="conditions">
        Partly Cloudy, 17 C
    </div>
    <div class="forecast">
        <b>Forecast:</b>
        Tue - PM Thunderstorms. High: 26 Low: 16
        Wed - Mostly Sunny. High: 27 Low: 16

        <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Pretoria__SF/*http://weather.yahoo.com/forecast/SFXX0044_c.html">Full Forecast at Yahoo! Weather</a></div>
    <div class="credit">
        provided by <a href="http://www.weather.com" >The Weather Channel</a>
    </div>
</div>

I'm not having any luck finding anything that'll guide me on how to do this.

Are there any api calls I can leverage to get a different presentation of my requested data?

If not, can anyone suggest how I could successfully rewrite this code? Maybe with a small example...

  • 写回答

1条回答 默认 最新

  • doulangbi6869 2013-03-12 08:25
    关注

    Grab the result html chunk you are getting, then use jquery to mod the crap out of it. Luckily the main div has a nice id. You should be able to grab individual chunks from your main html chunk, like

    <img ..../>, and dump it into a second html chunk where you add your <div>s with classes attached. Something along the lines of alert($('div#weather-feed').html()), this for example will print the entire chunk. Skipping ahead: alert($('div#weather-feed img').attr("src")) will get you the src of the img. Play around, use the html and rebuild it.

    Continued: Assuming you know or can figure out how jquery works and what document.ready() is. Jquery will be your friend here. I am posting the example blow. Look at this. It has hints all over it. How to mess with some html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料