giddyeffects / yii2-yiipixu
通过 JSON/XML RESTful Apixu API 在您的 Yii2 项目中直接访问天气和地理数据
dev-master
2017-04-05 09:51 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-29 01:12:36 UTC
README
通过 JSON/XML RESTful Apixu API 在您的 Yii2 项目中直接访问天气和地理数据
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist "giddyeffects/yii2-yiipixu":"@dev"
或将以下内容添加到您的 composer.json
文件的 require 部分中。
"giddyeffects/yii2-yiipixu": "@dev"
用法
首先在 这里 获取 Apixu API 密钥。
安装扩展后,只需在您的应用程序配置中添加以下代码
return [ //.... 'components' => [ //... 'apixu' => [ 'class' => 'giddyeffects\yiipixu\Apixu', 'api_key' => 'YOUR_APIXU_API_KEY', ], ], ];
现在您可以通过 \Yii::$app->apixu 访问扩展
更多详细信息请参阅 Apixu 文档。示例
$weather = \Yii::$app->apixu;
$weather->query = 'Nairobi';
$weather->request();
if(!$weather->response->error){
echo "<h1>Current Weather</h1>";
echo "<h2>Location</h2>";
echo "City: ". $weather->response->location->name;
echo "<br>";
echo "Region: ".$weather->response->location->region;
echo "<br>";
echo "Country: ".$weather->response->location->country;
echo "<br>";
echo "Lat: ".$weather->response->location->lat." , Long:".$weather->response->location->lon;
echo "<h2>Temperature</h2>";
echo "<br>";
echo "Temperature (°C): " . $weather->response->current->temp_c; echo "<br>";
echo "Feels like (°C)". $weather->response->current->feelslike_c;
echo "<br>";
echo "<br>";
echo "Temperature (°F): " . $weather->response->current->temp_f; echo "<br>";
echo "Feels like (°F)". $weather->response->current->feelslike_f;
echo "<br>";
echo "Condition: <img src='" . $weather->response->current->condition->icon ."'>" . $weather->response->current->condition->text;
echo "<h2>Wind</h2>";
echo $weather->response->current->wind_mph." mph <br>";
echo $weather->response->current->wind_kph." kph <br>";
echo $weather->response->current->wind_degree."° " . $weather->response->current->wind_dir."<br>";
echo "Humidity: ".$weather->response->current->humidity;
echo "<br><br><br>";
echo "Updated On: ".$weather->response->current->last_updated."<br/>";
}
else {
echo $weather->response->error->message;
}
$weather->api_method = 'forecast';
$weather->query = "Mombasa";
$weather->days = 3;
echo "<h1>Weather forecast for the next $weather->days days for $weather->query </h1><br/>";
$weather->request();
if(!$weather->response->error){
foreach ($weather->response->forecast->forecastday as $day) {
echo "<table>";
echo "<tr><td colspan='4' border='0'><h2>{$day->date}</h2> Sunrise: {$day->astro->sunrise} <br> Sunset: {$day->astro->sunset}"
. "<br> condition: {$day->day->condition->text} <img src=' {$day->day->condition->icon}'/></td></tr>";
echo "<tr><td> </td><td>Max.<br>Temperature</td><td>Min.<br>Temperature</td><td>Avg.<br>Temperature</td></tr>";
echo "<tr><td>°C</td><td>{$day->day->maxtemp_c}</td><td>{$day->day->mintemp_c}</td><td>{$day->day->avgtemp_c}</td></tr>";
echo "<tr><td>°F</td><td>{$day->day->maxtemp_f}</td><td>{$day->day->mintemp_f}</td><td>{$day->day->avgtemp_f}</td></tr>";
echo "<tr><td><h4>Wind</h4></td><td colspan='3'>{$day->day->maxwind_mph}Mph <br> {$day->day->maxwind_kph}kph </td></tr>";
foreach ($day->hour as $hr){
echo "<tr><td colspan='4' border='0'>";
echo "<table style='width:100%;'>";
echo "<tr><td>Time</td><td>Temperature</td><td>Wind</td><td>Humidity</td></tr>";
echo "<tr><td><div>{$hr->time}<img src=' {$hr->condition->icon}'/></div></td><td>{$hr->temp_c}°C<br>{$hr->temp_f}°F</td><td>{$hr->wind_mph}Mph <br> {$hr->wind_kph}kph</td><td>$hr->humidity</td></tr>";
echo "</table></tr></td>";
}
echo "</table> <br>";
}
}
else {
echo $weather->response->error->message;
}
在线演示
前往 交互式 API 探索器 测试 API。