A limited number of queries in the code.

  • 1
  • Problem
  • Updated 5 years ago
I have a problem I do not know how to limit the number to ask. Kożysta of code where the forecast is displayed based on IP. Is there any chance to enter there limitations as update every 30 minutes?
I send my code can anyone fall for anything.
I have a problem I do not know how to limit the number to ask. Kożysta of code where the forecast is displayed based on IP. Is there any chance to enter there limitations as update every 30 minutes?
I send my code can anyone fall for anything.

* @url http://davidcool.com
* @version 0.5
*/

class weather {

/**
* Api key from wunderground.com
*
* @var string
*/
private $apiKey = 'KEY';
private $get = 'conditions';

/**
* Your lang
*
* @var string
*/
private $lang = 'PL';

/**
* Initialize country or state
*
* @var string
*/
private $location = '';

/**
* Initialize city
*
* @var string
*/
private $city = '';

private $json_geo;
private $json;

public function __construct() {
$this->getAllWeather();
}

/**
* Get weather from api ,decode and save in $json
*/
private function getAllWeather() {
if ($this->location OR $this->city == "") {
$this->getGeoLoc();
}
$apiUrl = 'http://api.wunderground.com/api/' . $this->apiKey . '/' . $this->get . '/lang:PL/q/' . $this->location . '/' . $this->city . '.json?geo_ip=' . $this->getIP();
$this->json = json_decode(file_get_contents($apiUrl));
//dump($this->json);
}

/**
* Get geolocation from api ,decode and save in $json. Note: added ?geo_ip= to end of query, need to get client's IP address else it returns the hosting server's IP
*/
private function getGeoLoc() {
$geoUrl = 'http://api.wunderground.com/api/' . $this->apiKey . '/geolookup/lang:PL/q/autoip.json?geo_ip=' . $this->getIP();
$this->json_geo = json_decode(file_get_contents($geoUrl));
$this->location = $this->json_geo->location->state;
$this->city = str_replace(' ', '_', $this->json_geo->location->city);
}

/**
* Get current client IP address
*/
public function getIP() {
$currentIP = $_SERVER["REMOTE_ADDR"];
return $currentIP;
}

/**
* Get weather description
*
* @param bool $c
* @return string
*/
public function getWeatherDesc() {
return $this->json->current_observation->weather;
}

/**
* Get wind information
*
* @return string
*/
public function getWindDesc() {
return $this->json->current_observation->wind_dir;
}

public function getWindDesc1() {
return $this->json->current_observation->wind_gust_kph;
}

/**
* Get humidity information
*
* @return string
*/
public function getHumidity() {
return $this->json->current_observation->relative_humidity;
}

/**
* Get Precipitation information
*
* @param string $format
* @return string
*/
public function getPrecip($format = "CF") {
if ($format == "C") {
$out = $this->json->current_observation->precip_today_metric." mm";
} elseif ($format == "F") {
$out = $this->json->current_observation->precip_today_in." in";
} else {
$out = $this->json->current_observation->precip_today_string;
}

return $out;
return $this->json->current_observation->precip_today_string;
}

/**
* Get weather icon with html tag if $imgTag is true
*
* @param bool $imgTag add tag if true
* @return string
*/
public function getWeatherIcon($imgTag = false) {
$img = $this->json->current_observation->icon_url;
if ($imgTag) {
$img = "";
}
return $img;
}

/**
* Get temperature celcius if $c is true
*
* @param string $format
* @return string
*/
public function getTemperature($format = "CF") {
if ($format == "C") {
$out = round($this->json->current_observation->temp_c);
} elseif ($format == "F") {
$out = round($this->json->current_observation->temp_f);
} else {
$out = $this->json->current_observation->temperature_string;
}

return $out;
}

/**
* Get feels like
*
* @param string $format
* @return string
*/
public function getFeelsLike($format = "CF") {
if ($format == "C") {
$out = round($this->json->current_observation->feelslike_c)."&degC";
} elseif ($format == "F") {
$out = round($this->json->current_observation->feelslike_f)."&degF";
} else {
$out = $this->json->current_observation->feelslike_string;
}

return $out;
}

/**
* Get astronomy/moon information
*
* @param string $format
* @return string
*/
public function getAstronomy($format = "") {
$astronomy = json_decode(file_get_contents('http://api.wunderground.com/api/' . $this->apiKey . '/astronomy/lang:PL/q/' . $this->location . '/' . $this->city . '.json'));
switch($format){
case "sunrise":
$out = $astronomy->moon_phase->sunrise->hour.":".$astronomy->moon_phase->sunrise->minute." AM";
break;
case "sunset":
$out = ($astronomy->moon_phase->sunset->hour - 12).":".$astronomy->moon_phase->sunset->minute." PM";
break;
}

return $out;
}

/**
* Get hourly 10 day forecast information
*
* @param
* @return string
*/
public function getHourly10day() {
$Hourly10day = json_decode(file_get_contents('http://api.wunderground.com/api/' . $this->apiKey . '/hourly10day/lang:PL/q/' . $this->location . '/' . $this->city . '.json'));
$currentDay = $Hourly10day->hourly_forecast[0]->FCTTIME->yday;
$currentYear = $Hourly10day->hourly_forecast[0]->FCTTIME->year;
$daysInYear = date("z", mktime(0,0,0,12,31,$currentYear)) + 1;

foreach($Hourly10day as $a) {
$hours = $a;
}

$count = 1;
$dayCount = 0;
$hourSkip = 0;
$x = 0;
$searchDay = $currentDay;

while($x < 10) {
if($x == 0) {
while($dayCount < 10) {
$out[$x][$dayCount][yday].= $hours[$count+$dayCount]->FCTTIME->yday;
$out[$x][$dayCount][hour_padded].= $hours[$count+$dayCount]->FCTTIME->hour_padded;
$out[$x][$dayCount][ampm].= $hours[$count+$dayCount]->FCTTIME->ampm;
$out[$x][$dayCount][url].= $hours[$count+$dayCount]->icon_url;
$out[$x][$dayCount][temp].= $hours[$count+$dayCount]->temp->metric;
$dayCount++;
} // end while
$dayCount = 0;
$searchDay++;
} else {
while($count < count($hours)) {
if ($hours[$count]->FCTTIME->yday == $searchDay) {
while($dayCount < 10) {
$out[$x][$dayCount][yday].= $hours[$count+$hourSkip]->FCTTIME->yday;
$out[$x][$dayCount][hour_padded].= $hours[$count+$hourSkip]->FCTTIME->hour_padded;
$out[$x][$dayCount][ampm].= $hours[$count+$hourSkip]->FCTTIME->ampm;
$out[$x][$dayCount][url].= $hours[$count+$hourSkip]->icon_url;
$out[$x][$dayCount][temp].= $hours[$count+$hourSkip]->temp->metric;
$dayCount++;
$hourSkip = $hourSkip + 3;
} // end while
$dayCount = 0;
$hourSkip = 0;
if($searchDay == $daysInYear) {
$searchDay = 1;
} else {
$searchDay++;
}
break;
} // end if
$count++;
} // end while
} // end else
$x++;
} // end day while

return $out;

}

/**
* Get 10 day forecast information
*
* @param
* @return string
*/
public function getForecast10day() {
$Forecast10day = json_decode(file_get_contents('http://api.wunderground.com/api/' . $this->apiKey . '/forecast10day/lang:PL/q/' . $this->location . '/' . $this->city . '.json'));
//dump($Forecast10day);
for ($i = 0; $i forecast->simpleforecast->forecastday[$i]->high->fahrenheit;
$out[$i][Flow] = $Forecast10day->forecast->simpleforecast->forecastday[$i]->low->fahrenheit;
$out[$i][Chigh] = $Forecast10day->forecast->simpleforecast->forecastday[$i]->high->celsius;
$out[$i][Clow] = $Forecast10day->forecast->simpleforecast->forecastday[$i]->low->celsius;
$out[$i][icon_url] = $Forecast10day->forecast->simpleforecast->forecastday[$i]->icon_url;
$out[$i][weekday_short] = $Forecast10day->forecast->simpleforecast->forecastday[$i]->date->weekday_short;
}

return $out;
}

/**
* Get satelite .gif
*
* @param bool $imgTag
* @return string
*/
public function getSateliteImage() {
$img = file_get_contents('http://api.wunderground.com/api/' . $this->apiKey . '/animatedsatellite/q/' . $this->location . '/' . $this->city . '.gif?key=sat_ir4&basemap=1&timelabel=1&timelabel.y=10&num=8&delay=40&width=1000&height=1000');
$img = '';

return $img;
}

/**
* Get radar .gif
*
* @param bool $imgTag
* @return string
*/
public function getRadarImage() {
$img = file_get_contents('http://api.wunderground.com/api/' . $this->apiKey . '/animatedradar/q/' . $this->location . '/' . $this->city . '.gif?key=sat_ir4&basemap=1&timelabel=1&timelabel.y=10&num=8&delay=40');
$img = '';

return $img;
}

/**
* Set Language, http://www.wunderground.com/weather/a...
*
* @param string $lang
*/
public function setLang($lang) {
$this->lang = $lang;
}

/**
*
* @param string $location
* @param string $city
*/
public function setLocation($location, $city) {
$this->location = $location;
$this->city = $city;
}

/**
* Get city name and country/state
*
* @return string
*/
public function getLocation() {
return $this->json->current_observation->display_location->city;
}

/**
*
* @param string $apiKey
*/
public function setApiKey($apiKey) {
$this->apiKey = $apiKey;
}

}
?>
Photo of brzanek

brzanek

  • 5 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 122 Reply Likes
You can list several features together in 1 URL hit to cut down on your number of hits.

http://api.wunderground.com/api//geol...