Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) die();
// Настройки API
$apiKey = "f5af0830-4836-4252-ab64-27855f0c3e4c"; // Замените на ваш API-ключ
$latitude = "55.75396"; // Широта Москвы
$longitude = "37.620393"; // Долгота Москвы
// Настройки запроса
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'X-Yandex-Weather-Key: ' . $apiKey
)
);
$context = stream_context_create($opts);
// URL запроса к API Яндекс.Погоды
$url = "https://api.weather.yandex.ru/v2/forecast?lat={$latitude}&lon={$longitude}";
// Получение данных через file_get_contents
$response = file_get_contents($url, false, $context);
// Парсинг JSON
$data = json_decode($response, true);
// Проверка наличия данных
if (isset($data["fact"])) {
$arResult["WEATHER"] = [
"temperature" => $data["fact"]["temp"],
"condition" => $data["fact"]["condition"],
"humidity" => $data["fact"]["humidity"],
"wind_speed" => $data["fact"]["wind_speed"]
];
} else {
$arResult["WEATHER"] = null; // Ошибка при получении данных
}
// Подключение шаблона
$this->IncludeComponentTemplate();
?>