Page 1 sur 2
erreur de boucle foreach
Posté : 11 juil. 2021, 10:41
par flexi2202
bonjour a tous
j essaye de récupérer les information d un tableau de openweather
pour ce faire j utilise ce code
cela fonctionne
mais lorsque j affiche les erreurs en php
j ai ce message
Notice: Undefined property: stdClass::$hourly in /storage/ssd4/105/15003105/public_html/peche2/48h00accordeon.php on line 2050
Warning: Invalid argument supplied for foreach() in /storage/ssd4/105/15003105/public_html/peche2/48h00accordeon.php on line 2050
ces lignes correspondent a celle ci
Code : Tout sélectionner
<?php
//on recupere l api et on y mets la $ville que l on a trouve a la ligne 57
$url = "https://api.openweathermap.org/data/2.5/weather?q=$ville&lang=fr&units=metric&appid=e30eac7b1e60d534d109bfad";
//on va rechercher la latitude et longitude de cette ville car notre api suivante a besoin de ces infos pour afficher la meteo
$raw = file_get_contents($url);
$json = json_decode($raw);
foreach ($json->hourly as $hourly) {
}
$name=$json->name;
$lon=$json->coord->lon;
$lat=$json->coord->lat;
?>
Re: erreur de boucle foreach
Posté : 11 juil. 2021, 23:02
par @rthur
Ce message d'erreur indique que ton objet $json n'a pas de propriété hourly.
Fais un var_dump($json) pour vérifier ce que tu reçois
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:14
par flexi2202
merci pour l aide et voici ce que je reçois
Notice: Undefined property: stdClass::$daily in /storage/ssd4/105/15003105/public_html/peche2/48h00accordeon.php on line 2052
Code : Tout sélectionner
Warning: Invalid argument supplied for foreach() in /storage/ssd4/105/15003105/public_html/peche2/48h00accordeon.php on line 2052
object(stdClass)#2 (13) { ["coord"]=> object(stdClass)#1 (2) { ["lon"]=> float(4.3496) ["lat"]=> float(50.3778) } ["weather"]=> array(1) { [0]=> object(stdClass)#3 (4) { ["id"]=> int(800) ["main"]=> string(5) "Clear" ["description"]=> string(13) "ciel dégagé" ["icon"]=> string(3) "01n" } } ["base"]=> string(8) "stations" ["main"]=> object(stdClass)#4 (6) { ["temp"]=> float(15.37) ["feels_like"]=> float(15.31) ["temp_min"]=> float(14.18) ["temp_max"]=> float(16.95) ["pressure"]=> int(1014) ["humidity"]=> int(90) } ["visibility"]=> int(10000) ["wind"]=> object(stdClass)#5 (2) { ["speed"]=> float(2.06) ["deg"]=> int(110) } ["clouds"]=> object(stdClass)#6 (1) { ["all"]=> int(0) } ["dt"]=> int(1626040836) ["sys"]=> object(stdClass)#7 (5) { ["type"]=> int(2) ["id"]=> int(2008641) ["country"]=> string(2) "BE" ["sunrise"]=> int(1626061505) ["sunset"]=> int(1626119458) } ["timezone"]=> int(7200) ["id"]=> int(2800482) ["name"]=> string(9) "Landelies" ["cod"]=> int(200) }
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:22
par @rthur
Donc c'est confirmé, tu n'as pas de "hourly"
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:25
par flexi2202
merci pour l aide
mais je suis en quoi alors
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:29
par flexi2202
en fait j utilise deux api
et je viens de m percevoir que je les avais mélangée pour répondre
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:30
par flexi2202
ah attends je me suis trompe
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 00:35
par flexi2202
donc je reprends j ai ce message d erreur
Notice: Undefined property: stdClass::$daily in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2055
Warning: Invalid argument supplied for foreach() in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2055
au niveau de ce code
Code : Tout sélectionner
/on prends l api et on extrait les données
$url = "https://api.openweathermap.org/data/2.5/weather?q=$ville&lang=fr&units=metric&appid=e30eac7b1e60d534d109bfad513a4d9a";
$raw = file_get_contents($url);
$json = json_decode($raw);
foreach ($json->daily as $daily) {
}
$name=$json->name;
$lon=$json->coord->lon;
$lon=$json->coord->lat;
var_dump($json) ;
?>
voici le résultat avec le var-dump
object(stdClass)#2 (13) { ["coord"]=> object(stdClass)#1 (2) { ["lon"]=> float(4.3496) ["lat"]=> float(50.3778) } ["weather"]=> array(1) { [0]=> object(stdClass)#3 (4) { ["id"]=> int(800) ["main"]=> string(5) "Clear" ["description"]=> string(13) "ciel dégagé" ["icon"]=> string(3) "01n" } } ["base"]=> string(8) "stations" ["main"]=> object(stdClass)#4 (6) { ["temp"]=> float(15.16) ["feels_like"]=> float(15.16) ["temp_min"]=> float(13.62) ["temp_max"]=> float(16.95) ["pressure"]=> int(1013) ["humidity"]=> int(93) } ["visibility"]=> int(10000) ["wind"]=> object(stdClass)#5 (2) { ["speed"]=> float(2.06) ["deg"]=> int(110) } ["clouds"]=> object(stdClass)#6 (1) { ["all"]=> int(0) } ["dt"]=> int(1626042819) ["sys"]=> object(stdClass)#7 (5) { ["type"]=> int(2) ["id"]=> int(2008641) ["country"]=> string(2) "BE" ["sunrise"]=> int(1626061505) ["sunset"]=> int(1626119458) } ["timezone"]=> int(7200) ["id"]=> int(2800482) ["name"]=> string(9) "Landelies" ["cod"]=> int(200) }
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:29
par @rthur
Est-ce que tu vois un daily dans ton var_dump ?
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:36
par flexi2202
bonjour @rthur
merci pour l aide
juste
je viens de corriger
Code : Tout sélectionner
<?php
//on prends l api et on extrait les données
$url = "https://api.openweathermap.org/data/2.5/weather?q=$ville&lang=fr&units=metric&appid=e30eac7b1e60d534d109bfad9a";
$raw = file_get_contents($url);
$json = json_decode($raw);
foreach ($json->daily as $daily) {
}
$name=$json->name;
$lon=$json->coord->lon;
$lon=$json->coord->lat;
var_dump($json->daily) ;
?>
et le résultat est le suivant
Warning: file_get_contents(
https://api.openweathermap.org/data/2.5 ... ad513a4d9a): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2053
Notice: Trying to get property 'daily' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2055
Warning: Invalid argument supplied for foreach() in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2055
Notice: Trying to get property 'name' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2057
Notice: Trying to get property 'coord' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2058
Notice: Trying to get property 'lon' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2058
Notice: Trying to get property 'coord' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2059
Notice: Trying to get property 'lat' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2059
Notice: Trying to get property 'daily' of non-object in /storage/ssd4/105/15003105/public_html/peche2/dailyonecall.php on line 2060
NULL
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:40
par flexi2202
je pense que je ne devais pas faire de boucle avec daily mais avec quelle variable alors ?
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:42
par or 1
le message est explicite :
{"cod":"400","message":"Nothing to geocode"}
relire la doc de l'api pour voir ce que l'api retourne, si des paramètres passés à l'appel font que le retour est plus ou moins complet.
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:46
par flexi2202
bonjour or1
j ai pas très bien suivi ta réflexion sur ce coup la
ce qui m intéresse c est juste
Code : Tout sélectionner
$name=$json->name;
$lon=$json->coord->lon;
$lon=$json->coord->lat;
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:52
par or 1
foreach ($json->daily as $daily) {
}
imaginons que $json->daily existe. à quoi sert cette boucle ?
Re: erreur de boucle foreach
Posté : 12 juil. 2021, 08:57
par flexi2202
je dirais pas a grand chose
d autant plus que je dirais que $json->daily ne semble pas exister