Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // index.php
- <?php
- require_once("weather.php");
- $w = new weather();
- //$ipaddress = $_SERVER['REMOTE_ADDR'];
- $ipaddress = "127.0.0.1";
- $user = "ortund3354";
- $key = "35645644g56kj4g53f6gk3g6u3i";
- $locationstr = "http://api.locatorhq.com/?user=".$user."&key=".$key."&ip=".$ipaddress."&format=xml";
- $xml = simplexml_load_file($locationstr);
- $city = $xml->city;
- $region = $xml->region;
- $jhbIcon = $w->getWeatherIcon("SFXX0023");
- $dbnIcon = $w->getWeatherIcon("SFXX0011");
- $cptIcon = $w->getWeatherIcon("SFXX0010");
- $weather = $w->getWeather($city);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Weather</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
- <link href="weather.css" rel="stylesheet" />
- </head>
- <body>
- <div class="wrap">
- <div id="header"><div>Weather</div></div>
- <div id="weather">
- <span class="hidden" id="weather-city"><?php echo($city); ?></span>
- <span class="hidden" id="weather-jhbicon"><?php echo($jhbIcon); ?></span>
- <span class="hidden" id="weather-dbnicon"><?php echo($dbnIcon); ?></span>
- <span class="hidden" id="weather-cpticon"><?php echo($cptIcon); ?></span>
- <div class="weather-feed"><?php echo ($weather); ?></div>
- <div class="weather-more-button"><img src="images/bullet_arrow_down.png" /></div>
- </div>
- </div>
- <script src="js/weather.js"></script>
- <script>
- $(document).ready(function() {
- city = $("#weather-city").html();
- jhbIcon = $("#weather-jhbicon").html();
- dbnIcon = $("#weather-dbnicon").html();
- cptIcon = $("#weather-cpticon").html();
- $(".weather-feed").weather(city, jhbIcon, dbnIcon, cptIcon);
- });
- </script>
- </body>
- </html>
- // weather.php
- <?php
- class weather
- {
- function getWeather($city)
- {
- switch ($city)
- {
- case "Pretoria":
- $weather = $this->readWeather("SFXX0044");
- break;
- case "Johannesburg":
- $weather = $this->readWeather("SFXX0023");
- break;
- case "Durban":
- $weather = $this->readWeather("SFXX0011");
- break;
- default:
- $weather = $this->readWeather("SFXX0023");
- break;
- }
- return $weather;
- }
- function readWeather($loccode)
- {
- $doc = new DOMDocument();
- // http://xml.weather.yahoo.com/forecastrss/SFXX0023_c.xml
- $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");
- return $desc->item(0)->nodeValue;
- }
- }
- }
- //
- // Get weather icons
- //
- function getWeatherIcon($loccode)
- {
- $icoData = $this->readWeather($loccode);
- //$val = substr($icoData, strpos($icoData, "http") - 1, strpos($icoData, "\">", ));
- preg_match('@src="(.+?)"@', $icoData, $m);
- $val = $m[1];
- return $val;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement