Created
June 16, 2012 17:04
-
-
Save MikeRogers0/2941958 to your computer and use it in GitHub Desktop.
Getting the Weather in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. e.g: Portsmouth, England | |
$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']; | |
} | |
| |
} | |
// Example | |
$w = new Weather('50.799995', '-1.065545'); // Input the Latitude and Longitude | |
echo $w->getLocation()->getWeather()->sayHuman(); | |
// Ouput~: Portsmouth, England, PO4 8 | Partly Cloudy 4°C, Humidity: 93%, Wind: N at 8 mph | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('weather-class.inc.php'); // This has all the code. | |
$w = new Weather('50.799995', '-1.065545'); // Input the Latitude and Longitude | |
echo $w->getLocation()->getWeather()->sayHuman(); | |
// Ouput~: Portsmouth, England, PO4 8 | Partly Cloudy 4°C, Humidity: 93%, Wind: N at 8 mph | |
?> |
Parse error: syntax error, unexpected '' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in D:\xampp\htdocs\smashed\www\weather-class.inc.php on line 3
Notice: Trying to get property 'short_name' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 33
Notice: Trying to get property 'short_name' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 33
Notice: Trying to get property 'short_name' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 33
Warning: simplexml_load_file(): http://www.google.com/ig/api?weather=,,,50799995,-1065545:1: parser error : StartTag: invalid element name in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Warning: simplexml_load_file(): <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="de" in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Warning: simplexml_load_file(): http://www.google.com/ig/api?weather=,,,50799995,-1065545:1: parser error : Extra content at the end of the document in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Warning: simplexml_load_file(): <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="de" in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 16
Notice: Trying to get property 'weather' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 19
Notice: Trying to get property 'current_conditions' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 19
Notice: Trying to get property 'condition' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 19
Notice: Trying to get property 'weather' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 20
Notice: Trying to get property 'current_conditions' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 20
Notice: Trying to get property 'temp_c' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 20
Notice: Trying to get property 'weather' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 21
Notice: Trying to get property 'current_conditions' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 21
Notice: Trying to get property 'humidity' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 21
Notice: Trying to get property 'weather' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 22
Notice: Trying to get property 'current_conditions' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 22
Notice: Trying to get property 'wind_condition' of non-object in C:\xampp\htdocs\racemeter\scripts\php\weather-class.inc.php on line 22
, , | 0°C, ,
This is a pretty old script now (7 years!) & it looks like the google weather API has changed a lot since I wrote it.
If you're looking for a weather API, use https://darksky.net/dev/docs#overview
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP Syntax Check: Parse error: syntax error, unexpected '' (T_STRING), expecting function (T_FUNCTION) in your code on line 3
public $lat, $long, $weather, $location;