1

I've been working on a weather feed for my website.

I'm currently only able to get forecasts for the next 2 days. I want forecasts for the next 5 days.

Here's my code:

$ipaddress = $_SERVER['REMOTE_ADDR'];
$locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddress."&format=xml";

$xml = simplexml_load_file($locationstr);

$city = $xml->city;

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");

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

            $_SESSION["weather"] = $desc->item(0)->nodeValue;
        }
    }
}

I'd like to direct your attention to the lines that query for the weather:

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

// url resolves to http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c in this case

Searching google, I found this link which suggested I use this url instead:

$doc->load("http://xml.weather.yahoo.com/forecastrss/SFXX0044_c.xml");

While this also works and I see a 5 day forecast in the XML file, I still only see 2 days forecast on my site.

I have a feeling this is because I'm leveraging the channel child element found in the RSS feed, while the XML feed has no such child element.

If anyone can provide any insight here, I would really appreciate it.

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Why are you loading it with `SimpleXML`, not using it and then loading it again with `DOMDocument`? Personally I feel `SimpleXML` is easier for parsing RSS feeds but either way you should use one or the other. – Cfreak Mar 12 '13 at 14:43

1 Answers1

1

This is what I get for asking questions too early...

As I was looking over my code again, I noticed that I had the yahooapis URL referenced twice: once in the switch, and again in readWeather.

Having removed the redundant reference and updating the url as per the thread mentioned, I see that it does work now.

See updated code for reference:

switch ($city)
{
    case "Pretoria":
        $loccode = "SFXX0044";

        readWeather($loccode);
        break;
}

function readWeather($loccode)
{
    $doc = new DOMDocument();
    $doc->load("http://xml.weather.yahoo.com/forecastrss/".$loccode."_c.xml");

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

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

            $_SESSION["weather"] = $desc->item(0)->nodeValue;
        }
    }
}
Ortund
  • 8,095
  • 18
  • 71
  • 139