Методы получения данных о погоде в Мурри с примерами кода

  1. API OpenWeatherMap с Python:

    import requests
    import json
    def get_murree_weather():
    api_key = "YOUR_API_KEY"
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        "q": "Murree",
        "appid": api_key,
        "units": "metric"
    }
    response = requests.get(base_url, params=params)
    data = response.json()
    if response.status_code == 200:
        temperature = data["main"]["temp"]
        humidity = data["main"]["humidity"]
        description = data["weather"][0]["description"]
        return f"Temperature: {temperature}°C, Humidity: {humidity}%, Description: {description}"
    else:
        return "Failed to retrieve Murree weather information"
    print(get_murree_weather())

    Обязательно замените «YOUR_API_KEY» фактическим ключом API из OpenWeatherMap.

  2. API погоды с JavaScript:

    function getMurreeWeather() {
    const apiKey = "YOUR_API_KEY";
    const url = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=Murree`;
    fetch(url)
        .then(response => response.json())
        .then(data => {
            const temperature = data.current.temp_c;
            const humidity = data.current.humidity;
            const description = data.current.condition.text;
            console.log(`Temperature: ${temperature}°C, Humidity: ${humidity}%, Description: ${description}`);
        })
        .catch(error => console.log("Failed to retrieve Murree weather information"));
    }
    getMurreeWeather();

    Замените «YOUR_API_KEY» фактическим ключом API от WeatherAPI.

  3. API погоды Yahoo с PHP:

    function getMurreeWeather() {
    $api_key = "YOUR_API_KEY";
    $url = "https://weather-ydn-yql.media.yahoo.com/forecastrss";
    $params = array(
        "location" => "Murree",
        "format" => "json"
    );
    $headers = array(
        "Authorization: Bearer " . $api_key
    );
    $ch = curl_init($url . "?" . http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($response, true);
    if (isset($data["current_observation"])) {
        $temperature = $data["current_observation"]["condition"]["temperature"];
        $humidity = $data["current_observation"]["atmosphere"]["humidity"];
        $description = $data["current_observation"]["condition"]["text"];
        echo "Temperature: " . $temperature . "°C, Humidity: " . $humidity . "%, Description: " . $description;
    } else {
        echo "Failed to retrieve Murree weather information";
    }
    }
    getMurreeWeather();

    Замените «YOUR_API_KEY» фактическим ключом API от Yahoo Weather API.