Advertisement
Ortund

Untitled

Oct 10th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. // index.php
  2. <?php
  3.     require_once("weather.php");
  4.    
  5.     $w = new weather();
  6.        
  7.     //$ipaddress = $_SERVER['REMOTE_ADDR'];
  8.     $ipaddress = "127.0.0.1";
  9.     $user = "ortund3354";
  10.     $key = "35645644g56kj4g53f6gk3g6u3i";
  11.     $locationstr = "http://api.locatorhq.com/?user=".$user."&key=".$key."&ip=".$ipaddress."&format=xml";   
  12.     $xml = simplexml_load_file($locationstr);
  13.     $city = $xml->city;
  14.     $region = $xml->region;
  15.    
  16.     $jhbIcon = $w->getWeatherIcon("SFXX0023");
  17.     $dbnIcon = $w->getWeatherIcon("SFXX0011");
  18.     $cptIcon = $w->getWeatherIcon("SFXX0010");
  19.    
  20.     $weather = $w->getWeather($city);
  21. ?>
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25.     <title>Weather</title>
  26.    
  27.    
  28.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  29.    
  30.     <link href="weather.css" rel="stylesheet" />
  31. </head>
  32. <body>
  33.     <div class="wrap">
  34.         <div id="header"><div>Weather</div></div>
  35.         <div id="weather">
  36.             <span class="hidden" id="weather-city"><?php echo($city); ?></span>
  37.             <span class="hidden" id="weather-jhbicon"><?php echo($jhbIcon); ?></span>
  38.             <span class="hidden" id="weather-dbnicon"><?php echo($dbnIcon); ?></span>
  39.             <span class="hidden" id="weather-cpticon"><?php echo($cptIcon); ?></span>
  40.            
  41.             <div class="weather-feed"><?php echo ($weather); ?></div>
  42.             <div class="weather-more-button"><img src="images/bullet_arrow_down.png" /></div>
  43.         </div>
  44.     </div>
  45.        
  46.     <script src="js/weather.js"></script>
  47.     <script>
  48.         $(document).ready(function() {
  49.             city = $("#weather-city").html();
  50.             jhbIcon = $("#weather-jhbicon").html();
  51.             dbnIcon = $("#weather-dbnicon").html();
  52.             cptIcon = $("#weather-cpticon").html();
  53.        
  54.             $(".weather-feed").weather(city, jhbIcon, dbnIcon, cptIcon);
  55.         });
  56.     </script>
  57. </body>
  58. </html>
  59.  
  60. // weather.php
  61. <?php
  62.     class weather
  63.     {
  64.         function getWeather($city)
  65.             {
  66.                 switch ($city)
  67.                 {
  68.                     case "Pretoria":
  69.                         $weather = $this->readWeather("SFXX0044");
  70.                         break;
  71.                     case "Johannesburg":
  72.                         $weather = $this->readWeather("SFXX0023");
  73.                         break;
  74.                     case "Durban": 
  75.                         $weather = $this->readWeather("SFXX0011");
  76.                         break;
  77.                     default:
  78.                         $weather = $this->readWeather("SFXX0023");
  79.                         break;
  80.                 }
  81.                
  82.                 return $weather;
  83.             }
  84.        
  85.         function readWeather($loccode)
  86.         {
  87.             $doc = new DOMDocument();
  88.             //          http://xml.weather.yahoo.com/forecastrss/SFXX0023_c.xml
  89.             $doc->load("http://xml.weather.yahoo.com/forecastrss/".$loccode."_c.xml");
  90.            
  91.             $channel = $doc->getElementsByTagName("channel");
  92.            
  93.             $arr;
  94.             foreach($channel as $ch)
  95.             {
  96.                 $item = $ch->getElementsByTagName("item");
  97.                 foreach($item as $rcvd)
  98.                 {
  99.                     $desc = $rcvd->getElementsByTagName("description");
  100.                    
  101.                     return $desc->item(0)->nodeValue;
  102.                 }
  103.             }
  104.         }
  105.        
  106.         //
  107.         // Get weather icons
  108.         //
  109.         function getWeatherIcon($loccode)
  110.         {
  111.             $icoData = $this->readWeather($loccode);
  112.             //$val = substr($icoData, strpos($icoData, "http") - 1, strpos($icoData, "\">", ));
  113.             preg_match('@src="(.+?)"@', $icoData, $m);
  114.             $val = $m[1];
  115.             return $val;
  116.         }
  117.     }
  118.    
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement