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.