Answer the question
In order to leave comments, you need to log in
How to connect to the Yandex.Weather API?
Registered a test token. I'm trying to get a response, 403 Forbidden comes up. In the Yandex account, the statistics are clean.
AJAX request
<!DOCTYPE HTML>
<html>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script >
$.ajax({
url: 'https://api.weather.yandex.ru/v1/forecast?lat=56.852728&lon=53.213253',
headers: {
'X-Yandex-API-Key' : 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
},
type: 'GET',
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function(response) {
console.log(JSON.stringify(response));
}
});
</script>
</html>
<!DOCTYPE HTML>
<html>
<script>
try {
var xhttp = new XMLHttpRequest();
xhttp.open('GET', 'https://api.weather.yandex.ru/v1/forecast?lat=56.852728&lon=53.213253&extra=true', false);
xhttp.setRequestHeader('Content-type', 'text/html', 'X-Yandex-API-Key: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx');
xhttp.send();
var response = JSON.parse(xhttp.response);
console.log(xhttp.response);
}
catch (error) {
console.log(error.message);
}
</script>
</html>
Answer the question
In order to leave comments, you need to log in
<?php
header("Access-Control-Allow-Origin: *");
setlocale(LC_ALL, "ru_RU");
date_default_timezone_set("Europe/Moscow");
$opts = array(
'http' => array(
'method' => "GET",
'header' => "X-Yandex-API-Key: ----------- ваш ключ ----------"
)
);
$url = "https://api.weather.yandex.ru/v1/forecast?lat=52.886894&lon=40.509112&limit=1&hours=false&extra=false";
$context = stream_context_create($opts);
$contents = file_get_contents($url, false, $context);
$clima = json_decode($contents);
$time_unix = $clima->fact->obs_time;
$temp = $clima->fact->temp;
$humidity = $clima->fact->humidity;
$speed = $clima->fact->wind_speed;
$pressure = $clima->fact->pressure_mm;
$icon = $clima->fact->icon . ".svg";
$today = date("j/m/Y, H:i");
$time = date("j/m/Y, H:i", $time_unix);
echo "Дата/Вреемя:" . " - " . $today . "<br>";
echo "Обновлено:" . " - " . $time . "<br>";
echo "Температура: " . $temp . " °C<br>";
echo "Влажность: " . $humidity . " %<br>";
echo "Ветер: " . $speed . " м/с<br>";
echo "Давление: " . $pressure . " мм р/с<br>";
echo "<img src='https://yastatic.net/weather/i/icons/blueye/color/svg/" . $icon . "'/ width='50'>";
// var_dump(json_decode($contents));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question