Skip to content

Instantly share code, notes, and snippets.

@iantearle
Created April 18, 2013 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iantearle/5411803 to your computer and use it in GitHub Desktop.
Save iantearle/5411803 to your computer and use it in GitHub Desktop.
Forecast.io javascript library. Not currently working. Functions are not waiting for xhr data to complete. I have added a proxy script to help with cross domain policies. You should be able to run everything with these files. Please help me solve this. Many thanks. You can get your own API key from https://developer.forecast.io/register.
/**
* Helper Class for forecast.io webservice
*/
function ForecastIO(api_key) {
API_ENDPOINT = 'https://api.forecast.io/forecast/';
/**
* Create a new instance
*
* @param String $api_key
*/
ForecastIO.prototype = {
api_key: api_key
};
this.requestData = function(latitude, longitude) {
request_url = self.API_ENDPOINT + api_key + '/' + latitude + ',' + longitude + '?units=auto';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState==4 && xhr.status==200) {
content = xhr.responseText;
if(content != '' && (content)) {
return JSON.parse(content);
} else {
return false;
}
}
}
xhr.open('GET', 'proxy.php?url='+request_url, true);
xhr.send(null);
}
/**
* Will return the current conditions
*
* @param float $latitude
* @param float $longitude
* @return \ForecastIOConditions|boolean
*/
this.getCurrentConditions = function(latitude, longitude) {
data = this.requestData(latitude, longitude);
if(data !== false) {
return new ForecastIOConditions(data.currently);
} else {
return false;
}
}
/**
* Will return conditions on hourly basis for today
*
* @param type $latitude
* @param type $longitude
* @return \ForecastIOConditions|boolean
*/
this.getForecastToday = function(latitude, longitude) {
data = this.requestData(latitude, longitude);
if(data !== false) {
conditions = {};
today = date('Y-m-d');
for (var raw_dataVal in data) {
raw_data = data[raw_dataVal];
if(date('Y-m-d', raw_data.time) == today) {
conditions = new ForecastIOConditions(raw_data);
}
}
return conditions;
} else {
return false;
}
}
/**
* Will return daily conditions for next seven days
*
* @param float $latitude
* @param float $longitude
* @return \ForecastIOConditions|boolean
*/
this.getForecastWeek = function(latitude, longitude) {
data = this.requestData(latitude, longitude);
if(data !== false) {
conditions = {};
for (var raw_dataVal in data) {
raw_data = data[raw_dataVal];
conditions = new ForecastIOConditions(raw_data);
}
return conditions;
} else {
return false;
}
}
}
/**
* Wrapper for get data by getters
*/
function ForecastIOConditions() {
var raw_data;
ForecastIOConditions.prototype = {
raw_data: raw_data
}
/**
* Will return the temperature
*
* @return String
*/
this.getTemperature = function() {
return this.raw_data.temperature;
}
/**
* Get the summary of the conditions
*
* @return String
*/
this.getSummary = function() {
return this.raw_data.summary;
}
/**
* Get the icon of the conditions
*
* @return String
*/
this.getIcon = function() {
return this.raw_data.icon;
}
/**
* Get the time, when $format not set timestamp else formatted time
*
* @param String $format
* @return String
*/
this.getTime = function(format) {
if((format)) {
return this.raw_data.time;
} else {
return date(format, this.raw_data.time);
}
}
/**
* Get the pressure
*
* @return String
*/
this.getPressure = function() {
return this.raw_data.pressure;
}
/**
* get humidity
*
* @return String
*/
this.getHumidity = function() {
return this.raw_data.humidity;
}
/**
* Get the wind speed
*
* @return String
*/
this.getWindSpeed = function() {
return this.raw_data.windSpeed;
}
/**
* Get wind direction
*
* @return type
*/
this.getWindBearing = function() {
return this.raw_data.windBearing;
}
/**
* get precipitation type
*
* @return type
*/
this.getPrecipitationType = function() {
return this.raw_data.precipType;
}
/**
* get the probability 0..1 of precipitation type
*
* @return type
*/
this.getPrecipitationProbability = function() {
return this.raw_data.precipProbability;
}
/**
* Get the cloud cover
*
* @return type
*/
this.getCloudCover = function() {
return this.raw_data.cloudCover;
}
/**
* get the min temperature
*
* only available for week forecast
*
* @return type
*/
this.getMinTemperature = function() {
return this.raw_data.temperatureMin;
}
/**
* get max temperature
*
* only available for week forecast
*
* @return type
*/
this.getMaxTemperature = function() {
return this.raw_data.temperatureMax;
}
/**
* get sunrise time
*
* only available for week forecast
*
* @return type
*/
this.getSunrise = function() {
return this.raw_data.sunriseTime;
}
/**
* get sunset time
*
* only available for week forecast
*
* @return type
*/
this.getSunset = function() {
return this.raw_data.sunsetTime;
}
}
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="keywords" content="" />
<meta name="author" content="">
</head>
<body>
<script src="forecast.io.js"></script>
<script>
var api_key = 'API_KEY';
var latitude = '52.4308';
var longitude = '13.2588';
var forecast = new ForecastIO(api_key);
/*
* GET CURRENT CONDITIONS
*/
var condition = forecast.getCurrentConditions(latitude, longitude);
document.write(JSON.stringify(condition));
</script>
</body>
</html>
<?php
// File Name: proxy.php
if (!isset($_GET['url'])) die();
$url = urldecode($_GET['url']);
$url = 'https://' . str_replace('https://', '', $url); // Avoid accessing the file system
$url = file_get_contents($url);
print_r($url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment