8768 sujets

Développement web côté serveur, CMS

Bonjour,

Je souhaite parser les données météo de l'Api Accuweather.

Le lien  http://dataservice.accuweather.com/currentconditions/v1/2036394.json?apikey=(YourApiKey)&language=fr-fr&details=true  donne le resultat suivant:


[{"LocalObservationDateTime":"2018-03-21T00:24:00+00:00","EpochTime":1521591840,"WeatherText":"Ciel dégagé","WeatherIcon":33,"IsDayTime":false,"Temperature":{"Metric":{"Value":26.7,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"RealFeelTemperature":{"Metric":{"Value":26.4,"Unit":"C","UnitType":17},"Imperial":{"Value":79.0,"Unit":"F","UnitType":18}},"RealFeelTemperatureShade":{"Metric":{"Value":26.4,"Unit":"C","UnitType":17},"Imperial":{"Value":79.0,"Unit":"F","UnitType":18}},"RelativeHumidity":10,"DewPoint":{"Metric":{"Value":-7.6,"Unit":"C","UnitType":17},"Imperial":{"Value":18.0,"Unit":"F","UnitType":18}},"Wind":{"Direction":{"Degrees":0,"Localized":"N","English":"N"},"Speed":{"Metric":{"Value":9.8,"Unit":"km/h","UnitType":7},"Imperial":{"Value":6.1,"Unit":"mi/h","UnitType":9}}},"WindGust":{"Speed":{"Metric":{"Value":12.8,"Unit":"km/h","UnitType":7},"Imperial":{"Value":7.9,"Unit":"mi/h","UnitType":9}}},"UVIndex":0,"UVIndexText":"Minimum","Visibility":{"Metric":{"Value":16.1,"Unit":"km","UnitType":6},"Imperial":{"Value":10.0,"Unit":"mi","UnitType":2}},"ObstructionsToVisibility":"","CloudCover":1,"Ceiling":{"Metric":{"Value":9144.0,"Unit":"m","UnitType":5},"Imperial":{"Value":30000.0,"Unit":"ft","UnitType":0}},"Pressure":{"Metric":{"Value":1010.9,"Unit":"mb","UnitType":14},"Imperial":{"Value":29.85,"Unit":"inHg","UnitType":12}},"PressureTendency":{"LocalizedText":"Stationnaire","Code":"S"},"Past24HourTemperatureDeparture":{"Metric":{"Value":-0.6,"Unit":"C","UnitType":17},"Imperial":{"Value":-1.0,"Unit":"F","UnitType":18}},"ApparentTemperature":{"Metric":{"Value":23.9,"Unit":"C","UnitType":17},"Imperial":{"Value":75.0,"Unit":"F","UnitType":18}},"WindChillTemperature":{"Metric":{"Value":26.7,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"WetBulbTemperature":{"Metric":{"Value":11.0,"Unit":"C","UnitType":17},"Imperial":{"Value":52.0,"Unit":"F","UnitType":18}},"Precip1hr":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"PrecipitationSummary":{"Precipitation":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"PastHour":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past3Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past6Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past9Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past12Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past18Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past24Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}}},"TemperatureSummary":{"Past6HourRange":{"Minimum":{"Metric":{"Value":26.7,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":34.3,"Unit":"C","UnitType":17},"Imperial":{"Value":94.0,"Unit":"F","UnitType":18}}},"Past12HourRange":{"Minimum":{"Metric":{"Value":26.7,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":41.7,"Unit":"C","UnitType":17},"Imperial":{"Value":107.0,"Unit":"F","UnitType":18}}},"Past24HourRange":{"Minimum":{"Metric":{"Value":21.0,"Unit":"C","UnitType":17},"Imperial":{"Value":70.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":41.7,"Unit":"C","UnitType":17},"Imperial":{"Value":107.0,"Unit":"F","UnitType":18}}}},"MobileLink":"http://m.accuweather.com/fr/ml/kerere/2036394/current-weather/2036394?lang=fr-fr","Link":"http://www.accuweather.com/fr/ml/kerere/2036394/current-weather/2036394?lang=fr-fr"}]

Et donc pour parser les données, j'ai fait:

<?php
  $json_string = file_get_contents("http://dataservice.accuweather.com/currentconditions/v1/2036394.json?apikey=(YourApiKey)&language=fr-fr&details=true");
  $parsed_json = json_decode($json_string);
  $temp_c = $parsed_json->{'Temperature'}->{'Metric'}->{'Value'};
  $feelslike_c = $parsed_json->{'RealFeelTemperature'}->{'Metric'}->{'Value'};
  $weather = $parsed_json->{'WeatherText'};
  $humidite = $parsed_json->{'RelativeHumidity'};
  $ventkph = $parsed_json->{'Speed'}->{'Metric'}->{'Value'};
  $ventdir = $parsed_json->{'Wind'}->{'Direction'}->{'Localized'};
  $rafalekph = $parsed_json->{'current_observation'}->{'wind_gust_kph'};
  $visibilite = $parsed_json->{'Visibility'}->{'Metric'}->{'Value'};
  $degreduvent = $parsed_json->{'current_observation'}->{'wind_degrees'};
  $pointrosee = $parsed_json->{'DewPoint'}->{'Metric'}->{'Value'};
  $pressionatmos = $parsed_json->{'Pressure'}->{'Metric'}->{'Value'};
  $uv = $parsed_json->{'UVIndex'};
  $uvtext = $parsed_json->{'UVIndexText'};
  $precipitation = $parsed_json->{'PrecipitationSummary'}->{'Precipitation'}->{'Metric'}->{'Value'};
  $CloudCover = $parsed_json->{'CloudCover'};
?>


Mais ça ne marche pas.

Merci d'avance pour votre aide
Salut Smiley smile

bah la question la vraie est : où tu as appris à travailler sur un objet ?

$temp_c = $parsed_json->{'Temperature'}->{'Metric'}->{'Value'};

c'est quoi cette écriture ???

voilà la bonne écriture :

$temp_c = $parsed_json->Temperature->Metric->Value;
Salut,
Les deux notations sont possibles, avec et sans bracket.
Le problème vient d’ailleurs.

$json = json_decode('{
    "glossary": {
        "title": "example glossary"
    }
}');

dd($json->glossary->title, $json->{'glossary'}->title);

Modifié par Adwim (22 Mar 2018 - 09:48)
Bonjour,

Merci beaucoup pour vos réponses.

J'ai trouvé un script qui marche bien :

<?php
$json = file_get_contents('http://dataservice.accuweather.com/currentconditions/v1/2036394.json?apikey=(YourApiKey)&language=fr-fr&details=true');

 //decode JSON to array
 $data = json_decode($json,true);

 //show data
 echo "<pre>";
// print_r($data);
echo "</pre>";
 foreach ($data as $value) {
    echo $value["Temperature"]["Metric"]["Value"]."\n";
 }
foreach ($data as $value) {
    echo $value["UVIndex"]."\n";
 }
foreach ($data as $value) {
    echo $value["WeatherText"]."\n";
 }
//etc
 ?>


Mais je vais essayer le script donné par @Adwim
Modifié par Clement-b (22 Mar 2018 - 13:26)
Re Smiley cligne

DOnc ok les 2 notations sont valables.. jamasi vu la deuxième pour autant Smiley smile

Bref

Ton problème vient tout simplement de ta référence objet

si tu avais fait un print_r du décode tu aurais vu que l'ensemble de l'objet est contenu dans un array Smiley smile

$temp_c = $parsed_json[0]->{'Temperature'}->{'Metric'}->{'Value'};


La solution que tu utilises finalement est très bien aussi Smiley cligne