Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/giddyeffects/yii2-yiipixu

Yii2 Extension for the Apixu Weather API
https://github.com/giddyeffects/yii2-yiipixu

apixu-weather-api yii2-extension

Last synced: about 1 month ago
JSON representation

Yii2 Extension for the Apixu Weather API

Awesome Lists containing this project

README

        

Yii2 Extension for the Apixu Weather API
========================================
Access weather and geo data via the JSON/XML RESTful Apixu API directly in your Yii2 project

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist "giddyeffects/yii2-yiipixu":"@dev"
```

or add

```
"giddyeffects/yii2-yiipixu": "@dev"
```

to the require section of your `composer.json` file.

Usage
-----
First get an Apixu API key [here](https://www.apixu.com/signup.aspx).

Once the extension is installed, simply add the following code in your application configuration:

```php
return [
//....
'components' => [
//...
'apixu' => [
'class' => 'giddyeffects\yiipixu\Apixu',
'api_key' => 'YOUR_APIXU_API_KEY',
],
],
];
```
You can now access the extension via \Yii::$app->apixu;

For more details refer to the [Apixu Documentation](https://www.apixu.com/doc/).
Example
-------
```
$weather = \Yii::$app->apixu;
$weather->query = 'Nairobi';
$weather->request();
if(!$weather->response->error){
echo "

Current Weather

";
echo "

Location

";
echo "City: ". $weather->response->location->name;
echo "
";
echo "Region: ".$weather->response->location->region;
echo "
";
echo "Country: ".$weather->response->location->country;
echo "
";
echo "Lat: ".$weather->response->location->lat." , Long:".$weather->response->location->lon;
echo "

Temperature

";
echo "
";
echo "Temperature (°C): " . $weather->response->current->temp_c; echo "
";
echo "Feels like (°C)". $weather->response->current->feelslike_c;
echo "
";
echo "
";
echo "Temperature (°F): " . $weather->response->current->temp_f; echo "
";
echo "Feels like (°F)". $weather->response->current->feelslike_f;
echo "
";
echo "Condition: " . $weather->response->current->condition->text;
echo "

Wind

";
echo $weather->response->current->wind_mph." mph
";
echo $weather->response->current->wind_kph." kph
";
echo $weather->response->current->wind_degree."° " . $weather->response->current->wind_dir."
";
echo "Humidity: ".$weather->response->current->humidity;
echo "


";
echo "Updated On: ".$weather->response->current->last_updated."
";
}
else {
echo $weather->response->error->message;
}
$weather->api_method = 'forecast';
$weather->query = "Mombasa";
$weather->days = 3;
echo "

Weather forecast for the next $weather->days days for $weather->query


";
$weather->request();
if(!$weather->response->error){
foreach ($weather->response->forecast->forecastday as $day) {
echo "";
echo "

{$day->date}

Sunrise: {$day->astro->sunrise}
Sunset: {$day->astro->sunset}"
. "
condition: {$day->day->condition->text} ";
echo " Max.
TemperatureMin.
TemperatureAvg.
Temperature";
echo "°C{$day->day->maxtemp_c}{$day->day->mintemp_c}{$day->day->avgtemp_c}";
echo "°F{$day->day->maxtemp_f}{$day->day->mintemp_f}{$day->day->avgtemp_f}";
echo "

Wind

{$day->day->maxwind_mph}Mph
{$day->day->maxwind_kph}kph ";
foreach ($day->hour as $hr){
echo "";
echo "";
echo "TimeTemperatureWindHumidity";
echo "
{$hr->time}
{$hr->temp_c}°C
{$hr->temp_f}°F{$hr->wind_mph}Mph
{$hr->wind_kph}kph$hr->humidity";
echo "";
}
echo "
";
}
}
else {
echo $weather->response->error->message;
}
```

Live Demo
---------
Go to the [Interactive API Explorer](https://www.apixu.com/api-explorer.aspx) to test the API.