Skip to content

Instantly share code, notes, and snippets.

@ashokdhaduk
Forked from tanmay27vats/yahoo_weather.php
Created May 28, 2018 04:40
Show Gist options
  • Save ashokdhaduk/707fcbd9356228b7bdad7ad6725f4f10 to your computer and use it in GitHub Desktop.
Save ashokdhaduk/707fcbd9356228b7bdad7ad6725f4f10 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();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment