PHP code example of meteosource / meteosource_php

1. Go to this page and download the library: Download meteosource/meteosource_php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meteosource / meteosource_php example snippets



// Change this to your actual API key
const YOUR_API_KEY = 'abcdefghijklmnopqrstuvwxyz0123456789ABCD';
// Change this to your actual tier
const YOUR_TIER = 'flexi';

// Initialize the main Meteosource object
$meteosource = new Meteosource\Meteosource(YOUR_API_KEY, YOUR_TIER);


// Get the forecast for given point
$forecast = $meteosource->getPointForecast(
    null,  // You can specify place_id instead of lat+lon
    37.7775,  // Latitude of the point
    -122.416389,  // Longitude of the point
    ['current', 'hourly'],  // Defaults to '("current", "hourly")'
    'US/Pacific',  // Defaults to 'UTC', regardless of the point location
    'en',  // Defaults to 'en'
    'auto'  // Defaults to 'auto'
);


// Get the historical weather
$timeMachine = $meteosource->getTimeMachine(
    '2019-12-25', // You can also pass array of dates, which can be string or DateTime objects
    null, // Start date - you can specify the range for dates you need, instead of array
    null, // End date  - you can specify the range for dates you need, instead of array
    'london', // ID of the place you want the historical weather for
    null, // You can specify lat instead of place_id
    null, // You can specify lon instead of place_id
    'UTC', // Defaults to 'UTC', regardless of the point location
    'us' // Defaults to 'auto'
);


echo $forecast;  // <Forecast for lat: 37.7775, lon: -122.416389>
echo $timeMachine;  // <TimeMachine for lat: 51.50853, lon: -0.12574>


// You can access all of the attributes with object operator:
echo $forecast->lat; // 37.7775

// ... or with index operator:
echo $forecast['lon'];  // -122.416389

// There is also information about the elevation of the point and the timezone
echo $timeMachine->elevation;  // 82
echo $timeMachine->timezone;  // 'UTC'


// <Instance of SingleTimeData (current) with 17 member variables (cloud_cover,
//  dew_point, feels_like, humidity, icon, icon_num, irradiance, ozone,
//  precipitation, pressure, summary, temperature, uv_index, visibility,
// wind, wind_chill)>
echo $forecast->current;


echo $forecast->minutely  // null


// <Hourly data with 164 timesteps
//  from 2021-09-08T22:00:00 to 2021-09-15T17:00:00>
echo $forecast->hourly;


echo $forecast->alerts; // <Alerts data containing 4 alerts>
foreach($alerts as $alert) {
    // <Instance of SingleTimeData with 8 member variables
    //  (certainty, description, event, expires, headline, onset, sender, severity)>
    echo $alert;
}


// If you pass no parameter, it checks for current time
$forecast->alerts->getActive(); // returns list of SingleTimeData instances
// You can use either string...
$forecast->alerts->getActive('2022-03-08T22:00:00');
// ... or datetime (both tz-aware and naive)
$forecast->alerts->getActive(new DateTime('2022-03-08T22:00:00'));


echo $timeMachine->data  // <Instance of MultipleTimesData (time_machine) with 24 timesteps from 2019-12-25T00:00:00 to 2019-12-25T23:00:00>


$forecast->hourly[0];
$timeMachine->data[0];


// Get a date in near future for which there are data in the current API response
$currentDate = (new DateTime())->add(new DateInterval('PT1H'));
$forecast->hourly[$currentDate];

// Get historical weather
$timeMachine->data['2019-12-25T03:00:00']


// Get a date in near future for which there are data in the current API response
$currentDt = new DateTime("Y-m-d\TH:00:00");

// Index with DateTime
$forecast->hourly[$currentDt]->temperature;

// Get historical weather
$timeMachine->data[new DateTime('2019-12-25')];


$forecast->current->temperature;
$forecast->hourly[0]->temperature;
$timeMachine->data[0]->weather  // cloudy


$forecast->current->wind->speed;
bash
composer