Forecast.io API for PHP
- Framework agnostic
- Built in Laravel support
- Cache support to reduce API calls
- Provide your own cache store and http client, if required
You can register the ForecastServiceProvider to automatically use the Laravel built in cache functionality.
To do so, update the providers
array in your app/config/app.php
:
'providers' => array(
// ...
'Uberboom\Forecast\ForecastServiceProvider',
),
Next, update the class aliases in the aliases
array in your app/config/app.php
:
'providers' => array(
// ...
'Forecast' => 'Uberboom\Forecast\Facades\Forecast',
),
First, publish the configuration from the package:
php artisan config:publish uberboom/forecast
Get an API key at Forecast for Developers and add the API key to the app/config/packages/uberboom/forecast/config.php
file:
'api_key' => 'your-api-key-from-developer.forecast.io',
The package includes two difference HTTP client implementations: One is using the PHP Curl extension, the other one relies on file_get_contents()
, so make sure that either the Curl extension is loaded or allow_url_fopen
is enabled in your php.ini file.
'httpclient' => 'curl|file',
The package provides some Laravel Facade magic, so fetching the weather forecast is a piece of cake:
$weather = \Forecast::getWeatherByLocation($latitude, $longitude);
If you need to change the units, you can set the units used in the response by using the method setUnits()
:
$weather = \Forecast::setUnits(\Uberboom\Forecast\Forecast::UNITS_SI)->getWeatherByLocation($latitude, $longitude);
If you are not using Laravel, you have to set the API key manually:
$forecastApiKey = 'your-api-key-from-developer.forecast.io';
$forecast = new \Uberboom\Forecast\Forecast();
$forecast->setApiKey($forecastApiKey);
The package includes two difference HTTP client implementations: One is using the PHP Curl extension, the other one relies on file_get_contents
, so make sure that either the Curl extension is loaded or allow_url_fopen
is enabled in your php.ini file.
$forecast->setHttpClientWrapper(new \Uberboom\Forecast\HttpClient\Curl());
$forecast->setHttpClientWrapper(new \Uberboom\Forecast\HttpClient\File());
Currently, the package only includes an cache store implementation for the Laravel framework. If you want to use the package’s cache functionality, you need to build and inject your own cache store class, which has to implement the \Uberboom\Forecast\CacheStore\CacheStoreInterface
interface.
$forecast->setCacheStore(new YourCacheStore());
$forecast->getWeatherByLocation($latitude, $longitude);
If you need to change the units, you can set the units used in the response by using the method setUnits()
:
$weather = $forecast::setUnits(\Uberboom\Forecast\Forecast::UNITS_SI)->getWeatherByLocation($latitude, $longitude);
- Improve API documentation
- Provide unit tests