Hi Kareylo,
merci pour ta reponse!
En effet en faisant des tests, je me suis apercu que $results est en effet vide.
voici le code complet de la classe OpenWeather, j'ai aussi vu dans les messages SEB86 parle d'un changement a effectuer. Jai essaye mais le resultat est toujours le meme... [(https://www.grafikart.fr/tutoriels/curl-php-1138)]
<?php
class OpenWeather {
private $apiKey;
public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
}
public function getToday(string $city): ?array
{
$data = $this->callAPI("weather?q={$city}");
return [
'temp' => $data['main']['temp'],
'description' => $data['weather'][0]['description'],
'date' => new DateTime()
];
}
public function getForecast(string $city): ?array
{
$data = $this->callAPI("forecast/daily?q={$city}");
foreach($data['list'] as $day) {
$results[] = [
'temp' => $day['temp']['day'],
'description' => $day['weather'][0]['description'],
'date' => new DateTime('@' . $day['dt'])
];
}
return $results;
}
private function callAPI(string $endpoint): ?array
{
$curl = curl_init("http://api.openweathermap.org/data/2.5/{$endpoint}&units=metric&lang=fr&APPID={$this->apiKey}&units=metric&lang=fr");
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CAINFO => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'cert.cer',
CURLOPT_TIMEOUT => 1
]);
$data = curl_exec($curl);
if ($data === false || curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
return null;
}
return json_decode($data, true);
}
}
Le fichier meteo.php contenant mon APIKey
<?php
require_once 'class/OpenWeather.php';
$weather = new OpenWeather('7f5e587cc9d81bc6f413c3929161f1ea');
$forecast = $weather->getForecast('Montpellier,fr');
$today = $weather->getToday('Montpellier,fr');
require 'elements/header.php';
?>
<div class="container">
<ul>
<li>En ce moment <?= $today['description'] ?> <?= $today['temp'] ?>°C</li>
<?php foreach($forecast as $day): ?>
<li><?= $day['date']->format('d/m/Y') ?> <?= $day['description'] ?> <?= $day['temp'] ?>°C</li>
<?php endforeach ?>
</ul>
</div>
<?php require 'elements/footer.php';
Aussi, si je passe cette adresse
[(https://samples.openweathermap.org/data/2.5/weather?q=London&appid=7f5e587cc9d81bc6f413c3929161f1ea)]
ca me renvoie bien un tableau JSON
coord
lon -0.13
lat 51.51
weather
0
id 300
main "Drizzle"
description "light intensity drizzle"
icon "09d"
base "stations"
main
temp 280.32
pressure 1012
humidity 81
temp_min 279.15
temp_max 281.15
visibility 10000
wind
speed 4.1
deg 80
clouds
all 90
dt 1485789600
sys
type 1
id 5091
message 0.0103
country "GB"
sunrise 1485762037
sunset 1485794875
id 2643743
name "London"
cod 200
J'espere que mon explication est comprehensible ;)
Merci d'avance