|
| 1 | + |
| 2 | +# Weather |
| 3 | + |
| 4 | +基于百度地图接口的 PHP 天气信息组件。 |
| 5 | + |
| 6 | +## 安装 |
| 7 | + |
| 8 | +```sh |
| 9 | +$ composer require overtrue/weather -vvv |
| 10 | +``` |
| 11 | + |
| 12 | +## 配置 |
| 13 | + |
| 14 | +在使用本拓展之前,你需要去 [百度地图](http://lbsyun.baidu.com/index.php?title=car/api/weather) 注册账号,然后创建应用,获取应用的 `ak` 值 |
| 15 | + |
| 16 | +## 使用 |
| 17 | + |
| 18 | +```php |
| 19 | +use Overtrue\Weather\Weather; |
| 20 | + |
| 21 | +$ak = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; |
| 22 | + |
| 23 | +$weather = new Weather($ak); |
| 24 | + |
| 25 | +// 返回数组格式 |
| 26 | +$response = $weather->getWeather('深圳'); |
| 27 | + |
| 28 | +// 批量获取 |
| 29 | +$response = $weather->getWeather('深圳,北京'); |
| 30 | + |
| 31 | +// 返回 XML 格式 |
| 32 | +$response = $weather->getWeather('深圳', 'xml'); |
| 33 | + |
| 34 | +// 按坐标获取 |
| 35 | +$response = $weather->getWeather('116.306411,39.981839', 'json'); |
| 36 | + |
| 37 | +// 自定义坐标格式(coord_type) |
| 38 | +$response = $weather->getWeather('116.306411,39.981839', 'json', 'bd09ll'); |
| 39 | +``` |
| 40 | + |
| 41 | +### 参数说明 |
| 42 | +``` |
| 43 | +array | string getWeather(string $location, string $format = 'json', string $coordType = null) |
| 44 | +``` |
| 45 | + |
| 46 | +> - $location 地点,中文或者坐标地址,多个用斗角逗号隔开 |
| 47 | +> - $format 返回格式, `json`(默认)/`xml`, `json` 将会返回数组格式,`xml` 返回字符串格式。 |
| 48 | +> - $coordType 坐标格式,允许的值为`bd09ll`、`bd09mc`、`gcj02`、`wgs84`,默认为 `gcj02` 经纬度坐标。 |
| 49 | +> 详情说明请参考官方:http://lbsyun.baidu.com/index.php?title=car/api/weather |
| 50 | +
|
| 51 | +### 在 Laravel 中使用 |
| 52 | + |
| 53 | +在 Laravel 中使用也是同样的安装方式,配置写在 `config/services.php` 中: |
| 54 | + |
| 55 | +```php |
| 56 | + 'weather' => [ |
| 57 | + 'ak' => env('BAIDU_WEATHER_AK'), |
| 58 | + 'sn' => env('BAIDU_WEATHER_SN'), |
| 59 | + ], |
| 60 | +``` |
| 61 | + |
| 62 | +然后在 `.env` 中配置(`BAIDU_WEATHER_SN` 为可选): |
| 63 | + |
| 64 | +```env |
| 65 | +BAIDU_WEATHER_AK= |
| 66 | +BAIDU_WEATHER_SN= |
| 67 | +``` |
| 68 | + |
| 69 | +可以用两种方式来获取 `Overtrue\Weather\Weather` 实例: |
| 70 | + |
| 71 | +#### 方法参数注入 |
| 72 | + |
| 73 | +```php |
| 74 | + public function edit(Weather $weather) |
| 75 | + { |
| 76 | + $response = $weather->get('深圳'); |
| 77 | + } |
| 78 | +``` |
| 79 | + |
| 80 | +#### 服务名访问 |
| 81 | + |
| 82 | +```php |
| 83 | +$response = app('weather')->get('深圳'); |
| 84 | +``` |
| 85 | + |
| 86 | +## 参考 |
| 87 | + |
| 88 | +- [百度地图天气接口](http://lbsyun.baidu.com/index.php?title=car/api/weather) |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +MIT |
0 commit comments