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
Modérateur
Et l'eau,

Il te manque une clef d'API : (YourApiKey)

Ensuite, c'est simple (je me suis basé sur tes données json) :

<?php 
    $rows = json_decode(file_get_contents("weather.json"));
    echo '<pre>';
    foreach ($rows as $row) {
        print_r($row->LocalObservationDateTime);
    }
    echo '</pre>';
?>
Bonjour juste pour ma culture perso, ça correspond à quoi "LocalObservationDateTime" qu'on trouve dans la boucle for each ?
Modérateur
squeek a écrit :
Bonjour juste pour ma culture perso, ça correspond à quoi "LocalObservationDateTime" qu'on trouve dans la boucle for each ?


C'est une clef dans les data JSON