Quantcast
Channel: Geeklets
Viewing all articles
Browse latest Browse all 81

Weather using Yahoo (2016)

$
0
0

Save the code below to a file (WeatherCurrent.php) and then create a shell command Geeklet with the command:

php /pathtofile/WeatherCurrent.php

Change the path variables to where you have your weather icons stored.

Create an Image Geeklet with URL:

file://localhost/pathtofiles/Geeklets/Weather/CurrentCondition.png

PHP Code

<?php
$Location = 'Universal City, CA';
$pathtoimages = '/pathtofiles/Geeklets/Weather/ConditionIcons/';
$pathtocurrentimage = '/pathtofiles/Geeklets/Weather/CurrentCondition.png';`
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$Location.'")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
 $phpObj =  json_decode($json,true);
//print_r($phpObj);
$temp = $phpObj['query']['results']['channel']['item']['condition']['temp'];
$condition = $phpObj['query']['results']['channel']['item']['condition']['text'];
$weathericon = $phpObj['query']['results']['channel']['item']['condition']['code'];
$units = $phpObj['query']['results']['channel']['units']['temperature'];

$weatherimage = $pathtoimages.$weathericon.'.png';

copy($weatherimage, $pathtocurrentimage);

echo $condition.' '.$temp.'°'.$units."\n";
?>

Viewing all articles
Browse latest Browse all 81

Trending Articles