11

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

php /pathtofile/WeatherForcast.php

Change the Location variable

PHP Code

<?php
$Location = "Disneyland, CA";


$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);
//var_dump($phpObj);
echo "\033[1;31mWeather for $Location \033[0m\r";    
echo "\033[36m".$phpObj['query']['results']['channel']['lastBuildDate']."\033[0m\r";
$forcast = $phpObj['query']['results']['channel']['item']['forecast'];

foreach ($forcast as $daily) {
    echo $daily['day'].' '.$daily['low'].'° - '.$daily['high'].'° - '.$daily['text']."\r";
}  
?>

Comments

User Avatar
gunners4678 1195 days ago
Hi! Do I create the .php file with TextEditor?
User Avatar
Acacia460 1023 days ago
start with a plain text file or text editor won't let you save as a .php file...make sure your file is plain text then simply save it as *filename*.php and it should work
User Avatar
purplelavander 1192 days ago
At gunner4678 use Xcode and create a blank file and paste in saving with a php file extension.

Question though how do I change this to centigrade? Otherwise working great
User Avatar
ninjahippo 1191 days ago
Thanks for the update!

If you want to display only today's forecast change the following text from

foreach ($forcast as $daily) {

echo $daily['day'].' '.$daily['low'].'° - '.$daily['low'].'° - '.$daily['text']."\r";

}

to

echo 'Low: '.$forcast[0]['low'].'° - High: '.$forcast[0]['high'].'° - '.$forcast[0]['text']."\r";

Replace the [0] for each additional day you want to display.
User Avatar
surdacki 1189 days ago
For newbies could you please post a brief step by step? It would be much appreciated.

Thanks!
User Avatar
bluereef 1175 days ago
I used Dreamweaver to create the php..

How about some help!!

More details are needed.

Does this show 2- 3 day forecast with current icon's??
User Avatar
ninjahippo 1175 days ago
For current conditions:

$BASE_URL = "https://query.yahooapis.com/v1/public/yql";

$yql_query = 'select item.condition from weather.forecast where woeid = 12796236';

// $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);

//var_dump($phpObj);

// echo "\033[36m".$phpObj['query']['results']['channel']['lastBuildDate']."\033[0m\r";

$forcast = $phpObj['query']['results']['channel']['item']['condition'];

echo 'Current: '.$forcast['text'].', '.$forcast['temp'].'°F'."\r";

?>

For today & tomorrow:

$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);

//var_dump($phpObj);

// echo "\033[36m".$phpObj['query']['results']['channel']['lastBuildDate']."\033[0m\r";

$forcast = $phpObj['query']['results']['channel']['item']['forecast'];

echo 'Today: '.$forcast[0]['text'].', Low: '.$forcast[0]['low'].'°F, High: '.$forcast[0]['high'].'°F.'."\r";

echo 'Tomorrow: '.$forcast[1]['text'].', Low: '.$forcast[1]['low'].'°F, High: '.$forcast[1]['high'].'°F.'."\r";

?>
User Avatar
bluereef 1174 days ago
Thanks for your help, worked great.

How do I pull icons???

Wish I was smarter to do this myself LOL!!!
User Avatar
Acacia460 1023 days ago
I'm having the same problem. Got the weather and conditions displaying, but no icon. I saved the icons into a folder and entered that folder path in the 2-3 lines, but no icon. There is no error message or anything, it simply doesn't display the weather icon.
User Avatar
kdavies 1172 days ago
OMG thank you so much for this! I've been fighting with temp in Geeklet for 2 weeks and could only find old data. It was 75º and partly cloudy for 10 days... LOL. Oddly, today it IS 75º and partly cloudy. Seriously. But the was great. Very helpful!
User Avatar
kdavies 1172 days ago
Now if I could only get just the current temp so I know if I need to run home and bring the dogs in. :)

And, of course, I got that working by reading just a little further down. Perfection!
User Avatar
bluereef 1172 days ago
Current conditions with temp...

curl --silent "http://www.accuweather.com/en/us/rio-ranchoa-nm/87124/weather-forecast/37157_pc" | awk -F\' '/acm_RecentLocationsCarousel.push/{print$14", "$10"°" }'| head -1
User Avatar
kdavies 1167 days ago
Has anyone else noticed that the temp isn't always the same as what it shows on the website? Mine is off by as much as 2-3 degrees, which doesn't sound like much, but it really can be!
User Avatar
kdavies 1166 days ago
I'm close to having the image, I think. I've got it pulling the weather code for the current forecast, but I need it to pick up the image with that code name (code=23...image is 23.png, for example) and then GeekTool can simply show the image. Or perhaps have it copy the image with that name to 'temp.png' and have GeekTool show temp.png. I'm having trouble getting it to do that.
User Avatar
ejleon 1115 days ago
Nice code

Any way to use this for current location for travelers?
User Avatar
dogwithahat 939 days ago
Is there anyway to change it to ºC for us international users?
User Avatar
dogwithahat 939 days ago
I figured it out.

Replace the fourth line of code (below):

$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$Location.'")';

with this:

$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$Location.'") and u="c"';

It's the addition of:

and u="c"
User Avatar
dogwithahat 939 days ago
The stock code works but every now and then I get a:

Warning:Invalid argument supplied for foreach() in ...

Do you know what's causing this?

Log in to comment or register here.