5

Weather using Yahoo (2016)

Posted in Weather by macsupport 1320 days ago

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";
?>

Comments

User Avatar
purplelavander 1317 days ago
Can't get this to work getting a parsing error

Parse error: parse error, expecting `"identifier (T_STRING)"' or `"variable (T_VARIABLE)"' or `"number (T_NUM_STRING)"' in /Users/spare/Documents/geeklets/weathercurrent.php on line 15
User Avatar
cmuriel 1313 days ago
I was having the same issue... there is a typo on line 4 of the php code, remove the ' at the end of the line and it should work.
User Avatar
Razz 1317 days ago
Can you explain to me what exactly needs to be replaced? I am not a php guy, html is more my thing
User Avatar
matTmin 1307 days ago
In the PHP code, you need to replace : "/pathtofiles/Geeklets/Weather/ConditionIcons/" by the path of your icons (you can download new ones on DeviantArt, just search for "plain weather icons" for exemple.

then in the PHP, you need to change the path : "/pathtofiles/Geeklets/Weather/CurrentCondition.png" to where you have created your Geeklet Image.

Then change in the PHP, change "Universal City, CA" (second line) to "Paris, France" for exemple. (just check what is working for you in www.yahoo.com/news/weather )

Hope this helps.
User Avatar
matTmin 1307 days ago
It's working but what should I add if I want the temp in Celsius ?

EDIT :

I've managed to make it myself by adding this to the code :

//Converts Fahrenheit to Celsius and make it round

$Celsius = round(5/9*($temp-32), 0);

//And replace the echo with this one

echo $Celsius.'°'."C";

Thanks for the script !
User Avatar
SparsheeeG 1268 days ago
Hi!

I'm new to Geeklets, and I get a warning saying

"Warning: copy(/Users/*REDACTED*/Desktop/Personal/flat_colorful/png34.png): failed to open stream; No such file or directory in /Users/*REDACTED*/Desktop/Personal/WeatherCurrent.php on line 22"

Does anyone have a solution? Thanks!

Log in to comment or register here.