WELCOME TO TZUYU.NL

My website is all about my passion for gaming and webdevelopment. I will show my source code/projects here.
Also all my in-game information can be found on this site!

Currently viewing: Weather Class
Published: Thu 26th of December 2019

This class communicates with Google to check the weather at the given location specified by latitude/longitude or the name.
It then gets several attributes like temperature in °C, Humidity, Wind and the overall condition

Code Snippets

The class itself
<?php
class Weather {
  public $lat, $long, $weather, $location;
  private $weather_data, $location_data;

  public function __construct($lat=0.0, $long=0.0){
    $this->lat = (float) $lat;
    $this->long = (float) $long;
  }

  // Find out the local weather
  public function getWeather(){
    // Load the Weather data
    $this->weather_data = simplexml_load_file(
      'http://www.google.com/ig/api?weather=,,,'.(int)($this->lat*1000000).','.(int)($this->long*1000000));

    //var_dump($this->weather_data->weather);
    $this->weather['condition'] = (string)$this->weather_data->weather->current_conditions->condition["data"];
    $this->weather['temp_c'] = (int)$this->weather_data->weather->current_conditions->temp_c["data"];
    $this->weather['humidity'] = (string)$this->weather_data->weather->current_conditions->humidity["data"];
    $this->weather['wind_condition'] = (string)$this->weather_data->weather->current_conditions->wind_condition["data"];
    return $this;
  }

  // Get the nearest weather hub location
  public function getLocation(){
    // Load the location data
    $this->location_data = simplexml_load_file(
    'http://maps.google.com/maps/api/geocode/xml?sensor=true&latlng='.($this->lat).','.($this->long));

    // Set the name based on the location.
    $this->location = $this->location_data->result->address_component[2]->short_name.', '.$this->location_data->result->address_component[4]->short_name.', '.$this->location_data->result->address_component[6]->short_name;
    return $this;
  }

  public function sayHuman(){
    return $this->location.' | '.
    $this->weather['condition'].' '.
    $this->weather['temp_c'].'°C, '.
    $this->weather['humidity'].', '.
    $this->weather['wind_condition'];
  }

}
?>