Skip to content

Instantly share code, notes, and snippets.

@tanmay27vats
Last active July 23, 2018 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanmay27vats/cfd451b23cf58b6e2f65b30d4ca1f6bf to your computer and use it in GitHub Desktop.
Save tanmay27vats/cfd451b23cf58b6e2f65b30d4ca1f6bf to your computer and use it in GitHub Desktop.
Yahoo weather YQL API without authentication / No OAuth required - PHP script
<?php
class Yahoo_Weather
{
protected $base_url = "http://query.yahooapis.com/v1/public/yql";
protected $location = false;
protected $units = false;
protected $current = false;
protected $future = false;
protected $data = false;
public function __construct($location)
{
if($location != "")
{
$this->location = $location;
$this->getWeather();
}
}
private function getWeather()
{
if($this->location)
{
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$this->location.'")';
$yql_query_url = $this->base_url . "?q=" . urlencode($yql_query) . "&format=json";
$curl = curl_init($yql_query_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($curl);
$data = json_decode($json);
$this->data = $data->query;
$this->units = $data->query->results->channel->units;
$this->current = $data->query->results->channel->item->condition;
$this->future = $data->query->results->channel->item->forecast;
}
}
}
class Display_Weather extends Yahoo_Weather
{
private $temp = false;
public function __construct($location, $temp)
{
try {
switch($temp)
{
case "c":
case "f":
parent::__construct($location);
$this->temp = $temp;
break;
default:
throw new Exception("Temperature must be in either C or F.");
break;
}
}
catch (Exception $e)
{
echo $e->getMessage(), "\n";
}
}
public function displayCurrentWeather()
{
if($this->current != false)
{
echo '<div class="wthr-wrapper text-center">';
echo '<div class="icon-wrap"><img src="path/to/images/weather-icons/'.$this->current->code.'.png" /></div>';
echo '<div class="text-cht-wthr current">'.$this->getTemp($this->current->temp).'</div>';
echo '<div class="wthr-txt-wrap">'.$this->current->text.'</div>';
echo '</div>';
}
}
public function displayFutureWeather()
{
if($this->future != false)
{
foreach ($this->future as $key => $day_weather)
{
echo '<div class="wthr-wrapper text-center">';
echo '<div class="date-wrap">'.$day_weather->date.'</div>';
echo '<div class="icon-wrap"><img src="path/to/images/weather-icons/'.$day_weather->code.'.png" /></div>';
echo '<div class="text-cht-wthr high">High: '.$this->getTemp($day_weather->high).'</div>';
echo '<div class="text-cht-wthr low">Low: '.$this->getTemp($day_weather->low).'</div>';
echo '<div class="wthr-txt-wrap">'.$day_weather->text.'</div>';
echo '</div>';
}
}
}
private function getTemp($temperature)
{
if($this->temp == "c")
{
return round(($temperature - 32) /1.8) . "&deg; C";
}
return $temperature."&deg; F";
}
}
$weather = new Display_Weather("chicago, il", "c");
$weather->displayCurrentWeather();
$weather->displayFutureWeather();
?>
@mtx-z
Copy link

mtx-z commented Jul 23, 2018

Using public API endpoint will limit your API call rate limit compared to an authenticated request (Oauth).

  • 2000/day on public endpoint (limitation is IP based).
  • 20.000/day when using Oauth (with your keys)

https://developer.yahoo.com/weather/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment