views:

123

answers:

4

Does anyone how to display in php the weather by date?

This is what I tried

Nothing yet with:

<?php

$url="http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&amp;format=json&amp;num_of_days=5&amp;key=8f2d1ea151085304102710";

$json = file_get_contents($url); $data = json_decode($json, TRUE);

echo $data[0]->weather->weatherIconUrl[0]->value;

?>

{ "data": { "current_condition": [ {"cloudcover": "31", "humidity": "36", "observation_time": "04:06 PM", "precipMM": "0.0", "pressure": "1021", "temp_C": "6", "temp_F": "43", "visibility": "10", "weatherCode": "143",  "weatherDesc": [ {"value": "Mist" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0006_mist.png" } ], "winddir16Point": "NE", "winddirDegree": "40", "windspeedKmph": "15", "windspeedMiles": "9" } ],  

"request": [ {"query": "Schruns, Austria", "type": "City" } ], 

 "weather": 
[ 
{"date": "2010-10-27", "precipMM": "0.0", "tempMaxC": "3", "tempMaxF": "38", "tempMinC": "-13", "tempMinF": "9", "weatherCode": "113",  "weatherDesc": [ {"value": "Sunny" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point": "N", "winddirDegree": "356", "winddirection": "N", "windspeedKmph": "5", "windspeedMiles": "3" }, 

{"date": "2010-10-28", "precipMM": "0.0", "tempMaxC": "5", "tempMaxF": "42", "tempMinC": "-8", "tempMinF": "18", "weatherCode": "113",  "weatherDesc": [ {"value": "Sunny" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point": "N", "winddirDegree": "2", "winddirection": "N", "windspeedKmph": "9", "windspeedMiles": "6" },
+3  A: 

Try this example

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

http://php.net/manual/en/function.json-decode.php

NB - two negatives makes a positive . :)

zod
+2  A: 

Seems like you forgot the ["value"] or ->value:

echo $data[0]->weather->weatherIconUrl[0]->value;
mario
+3  A: 

If you use the following instead:

$json = file_get_contents($url);
$data = json_decode($json, TRUE);

The TRUE returns an array instead of an object.

esryl
+1  A: 

This appears to work:

<?php

$url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&amp;format=json&amp;num_of_days=5&amp;key=8f2d1ea151085304102710%22';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json['data']['weather'] as $item) {
    print $item['date'];
    print ' - ';
    print $item['weatherDesc'][0]['value'];
    print ' - ';
    print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
    print '<br>';
}

If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.

Max