Advertisement
reenadak

get weather

Feb 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1.     /*
  2.     - Get Weather for location (openweathermap)
  3.     - Gets the weather, temperature, pressure, humidity etc for the specified location
  4.    
  5.     $location     - The location to get the weather of ie. "New York City,NY"
  6.     $format       - What to return
  7.                     => 0 - Cloudiness
  8.                     => 1 - Temperature (fahrenheit)
  9.                     => 2 - Air Pressure
  10.                     => 3 - Humidity
  11.                     => 4 - Min Temperature (fahrenheit)
  12.                     => 5 - Max Temperature (fahrenheit)
  13.                     => 6 - Visibility
  14.                     => 7 - Wind Speed
  15.                     => 8 - Wind Direction (degrees)
  16.                     => 9 - Sunrise (unix time)
  17.                     => 10 - Sunset (unix time)
  18.    
  19.     $timeout     - How many seconds to wait for a response before giving up
  20.     */
  21.    
  22.     public function getWeather($location, $format = 0, $timeout = 5) {
  23.         $ch = curl_init();
  24.         $location = str_replace(" ", "%20", $location);
  25.        
  26.         curl_setopt($ch, CURLOPT_URL, "http://api.openweathermap.org/data/2.5/weather?q=" . $location);
  27.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  28.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  29.        
  30.         $data = curl_exec($ch);
  31.         curl_close($ch);
  32.        
  33.         $arr = json_decode($data, true);
  34.         if (!isset($arr["message"])) {
  35.             $r_weather = $arr["weather"][0]["description"];
  36.             $r_temp = $arr["main"]["temp"];
  37.             $r_pressure = $arr["main"]["pressure"];
  38.             $r_humidity = $arr["main"]["humidity"];
  39.             $r_temp_min = $arr["main"]["temp_min"];
  40.             $r_temp_max = $arr["main"]["temp_max"];
  41.             $r_visibility = 10000;
  42.             $r_wind_speed = $arr["wind"]["speed"];
  43.             $r_wind_dir = $arr["wind"]["deg"];
  44.             $r_sunrise = $arr["sys"]["sunrise"];
  45.             $r_sunset = $arr["sys"]["sunset"];
  46.            
  47.             $r_temp = $this->convertTemperature("k", "f", $r_temp);
  48.             $r_temp_min = $this->convertTemperature("k", "f", $r_temp_min);
  49.             $r_temp_max = $this->convertTemperature("k", "f", $r_temp_max);
  50.  
  51.             switch ($format) {
  52.                 case 0: return $r_weather; break;
  53.                 case 1: return $r_temp; break;
  54.                 case 2: return $r_pressure; break;
  55.                 case 3: return $r_humidity; break;
  56.                 case 4: return $r_temp_min; break;
  57.                 case 5: return $r_temp_max; break;
  58.                 case 6: return $r_visibility; break;
  59.                 case 7: return $r_wind_speed; break;
  60.                 case 8: return $r_wind_dir; break;
  61.                 case 9: return $r_sunrise; break;
  62.                 case 10: return $r_sunset; break;  
  63.             }
  64.            
  65.             // default
  66.             return $arr;
  67.         }
  68.        
  69.         return false;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement