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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?