reenadak icon

get weather

reenadak | PRO | 02/20/18 06:20:15 PM UTC | 0 ⭐ | 371 👁️ | Never ⏰ | []
PHP |

2.87 KB

|

None

|

0 👍

/

0 👎

    /*
    - Get Weather for location (openweathermap)
    - Gets the weather, temperature, pressure, humidity etc for the specified location
   
    $location     - The location to get the weather of ie. "New York City,NY"
    $format       - What to return
                    => 0 - Cloudiness
                    => 1 - Temperature (fahrenheit)
                    => 2 - Air Pressure
                    => 3 - Humidity
                    => 4 - Min Temperature (fahrenheit)
                    => 5 - Max Temperature (fahrenheit)
                    => 6 - Visibility
                    => 7 - Wind Speed
                    => 8 - Wind Direction (degrees)
                    => 9 - Sunrise (unix time)
                    => 10 - Sunset (unix time)
   
    $timeout     - How many seconds to wait for a response before giving up
    */
   
    public function getWeather($location, $format = 0, $timeout = 5) {
        $ch = curl_init();
        $location = str_replace(" ", "%20", $location);
       
        curl_setopt($ch, CURLOPT_URL, "http://api.openweathermap.org/data/2.5/weather?q=" . $location);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
       
        $data = curl_exec($ch);
        curl_close($ch);
       
        $arr = json_decode($data, true);
        if (!isset($arr["message"])) {
            $r_weather = $arr["weather"][0]["description"];
            $r_temp = $arr["main"]["temp"];
            $r_pressure = $arr["main"]["pressure"];
            $r_humidity = $arr["main"]["humidity"];
            $r_temp_min = $arr["main"]["temp_min"];
            $r_temp_max = $arr["main"]["temp_max"];
            $r_visibility = 10000;
            $r_wind_speed = $arr["wind"]["speed"];
            $r_wind_dir = $arr["wind"]["deg"];
            $r_sunrise = $arr["sys"]["sunrise"];
            $r_sunset = $arr["sys"]["sunset"];
           
            $r_temp = $this->convertTemperature("k", "f", $r_temp);
            $r_temp_min = $this->convertTemperature("k", "f", $r_temp_min);
            $r_temp_max = $this->convertTemperature("k", "f", $r_temp_max);
 
            switch ($format) {
                case 0: return $r_weather; break;
                case 1: return $r_temp; break;
                case 2: return $r_pressure; break;
                case 3: return $r_humidity; break;
                case 4: return $r_temp_min; break;
                case 5: return $r_temp_max; break;
                case 6: return $r_visibility; break;
                case 7: return $r_wind_speed; break;
                case 8: return $r_wind_dir; break;
                case 9: return $r_sunrise; break;
                case 10: return $r_sunset; break;  
            }
           
            // default
            return $arr;
        }
       
        return false;
    }

Comments