/OpenWeatherMap-PHP-API

A PHP API to parse weather data and weather history from OpenWeatherMap.org.

Primary LanguagePHPMIT LicenseMIT

OpenWeatherMap PHP API

A PHP 7.0+ API to retrieve and parse global weather data from OpenWeatherMap.org. This library aims to normalise the provided data and remove some inconsistencies. This library is neither maintained by OpenWeatherMap nor their official PHP API.

Build Status license release codecov Scrutinizer Quality Score
SensioLabsInsight

Installation

This library can be found on Packagist. The recommended way to install and use it is through Composer.

composer require "cmfcmf/openweathermap-php-api"

You will also need to choose and install two additional dependencies separately:

  1. A PSR-17 compatible HTTP factory implementation. A list of HTTP factory implementations is available at Packagist.
  2. A PSR-18 compatible HTTP client implementation. A list of HTTP client implementations is available at Packagist.

Example call

<?php
use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
use Http\Factory\Guzzle\RequestFactory;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

// Must point to composer's autoload file.
require 'vendor/autoload.php';

// Language of data (try your own language here!):
$lang = 'de';

// Units (can be 'metric' or 'imperial' [default]):
$units = 'metric';

// You can use every PSR-17 compatible HTTP request factory
// and every PSR-18 compatible HTTP client. This example uses
// `http-interop/http-factory-guzzle` ^1.0 and `php-http/guzzle6-adapter` ^2.0 || ^1.0
// which you need to install separately.
$httpRequestFactory = new RequestFactory();
$httpClient = GuzzleAdapter::createWithConfig([]);

// Create OpenWeatherMap object.
$owm = new OpenWeatherMap('YOUR-API-KEY', $httpClient, $httpRequestFactory);

try {
    $weather = $owm->getWeather('Berlin', $units, $lang);
} catch(OWMException $e) {
    echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
} catch(\Exception $e) {
    echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
}

echo $weather->temperature;

For more example code and instructions on how to use this library, please take a look into the Examples folder. Make sure to get an API Key from http://home.openweathermap.org/ and put it into Examples/ApiKey.ini.

  • CurrentWeather.php shows how to receive the current weather.
  • WeatherForecast.php shows how to receive weather forecasts.
  • UVIndex.php shows how to receive uv index data.
  • AirPollution.php show how to receive air pollution data.

Contributing

I'm happy about every pull request or issue you find and open to help make this API more awesome.

Vagrant

You can use Vagrant to kick-start your development. Simply run vagrant up and vagrant ssh to start a PHP VM with all dependencies included.

Docker

You can also use Docker to start developing this library. First install dependencies:

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --user $(id -u):$(id -g) \
    composer install

And then execute an example:

docker run --rm --interactive --tty \
    --volume $PWD:/app -w /app \
    php bash

> php Examples/CurrentWeather.php

License

MIT — Please see the LICENSE file distributed with this source code for further information regarding copyright and licensing.

Please check out the following official links to read about the terms, pricing and license of OpenWeatherMap before using the service: