Download the PHP package meteosource/meteosource_php without Composer
On this page you can find all versions of the php package meteosource/meteosource_php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meteosource/meteosource_php
More information about meteosource/meteosource_php
Files in meteosource/meteosource_php
Package meteosource_php
Short Description PHP wrapper library for global weather API - hyperlocal weather forecasts
License MIT
Homepage https://github.com/Meteosource/metesource_php
Informations about the package meteosource_php
meteosource - Weather API library
PHP wrapper library for Meteosource weather API that provides detailed hyperlocal weather forecasts for any location on earth.
Installation
Install using Composer.
Get started
To use this library, you need to obtain your Meteosource API key. You can sign up or get the API key of existing account in your dashboard.
Library usage
Initialization
To initialize the Meteosource
object, you need your API key and the name of your subscription plan (tier). Basic example of initialization is shown below:
Get the weather data
Using meteosource
library, you can get weather forecasts or archive weather data (if you have a paid subscription).
Forecast
To get the weather data for given place, use getPointForecast()
method of the Meteosource
object. You have to specify either the coordinates of the place (lat
+ lon
) or the place_id
. Detailed description of the parameters can be found in the API documentation.
Note that the default timezone is always UTC
, as opposed to the API itself (which defaults to the point's local timezone). This is because the library always queries the API for the UTC
timezone to avoid ambiguous datetimes problems. If you specify a different timezone, the library still requests the API for UTC
, and then converts the datetimes to desired timezone.
Historical weather
Users with paid subscription to Meteosource can retrieve historical weather from time_machine
endpoint, using getTimeMachine()
method:
Note, that the historical weather data are always retrieved for full UTC days. If you specify a different timezone, the datetimes get converted, but they will cover the full UTC, not the local day. If you specify a datetime
to any of the date parameters, the hours, minutes, seconds and microseconds get ignored. So if you request '2021-12-25T23:59:59'
, you get data for full UTC day 2021-12-25
.
If you pass array
of dates to date
parameter, they days will be inserted into the inner structures in the order they are being iterated over. This affects time indexing by integer (see below). An API request is made for each day, even when you specify a date range.
Working with the weather data
All of the meteosource's data objects have overloaded __toString()
methods, so you can echo
the objects to get useful information about them:
Attribute access
The library loads the JSON response into its internal structures. You can access the attributes using the object operator (->
), or the index operator ([]
):
Weather data sections
There are 5 weather forecast sections (current
, minutely
, hourly
, daily
and alerts
) as attributes in the Forecast
object.
The current
data contains data for many variables for a single point in time (it is represented by SingleTimeData
object):
The minutely
, hourly
and daily
sections contain forecasts for more points in time (represented by MultipleTimesData
). The sections that were not requested are empty (null):
The sections that were requested can also be echo
ed, to view number of available timesteps and their range (inclusive):
The alerts
section contain meteorological alerts and warnings, if there are any issued for the location. The alerts
object is an instance of AlertsData
class. You can echo the object or iterate over it:
You can also get list of all active alerts for given time. If you use string
or tz-naive DateTime
in this function, it will suppose that it is in the same timezone as requested for the forecast.
There is a single section data
for historical weather as an attribute in the TimeMachine
object, represented by MultipleTimesData
.
Time indexing
As mentioned above, the minutely
, hourly
, daily
sections of Forecast
and the data
section of TimeMachine
contain data for more timesteps. To get the data for a single time, you have several options.
1. Indexing with integer
You can simply index an instance of MultipleTimesData
with integer
, as the offset from the current time:
2. Indexing with string
To get the exact time, you can use string
in YYYY-MM-DDTHH:00:00
format. The datetime string is assumed to be in the same timezone as the data.
3. Indexing with datetime
You can also use DateTime
as an index, it is automatically converted to the timezone of the data.
Note: minutely
, hourly
, daily
and alerts
are of ArrayIterator type
Variable access
To access the variable, you can need to use the object operator (->
):
Some variables are grouped into logical groups, just like in the API response. You can access the actual data with chained object operators (->
) operators:
Tests
The unit tests are written using PHPUnit
. You need to provide your actual API key using environment variable. To run the tests, use:
Contact us
You can contact us here.