项目作者: overtrue

项目描述 :
:rainbow: 基于高德开放平台接口的 PHP 天气信息组件。
高级语言: PHP
项目地址: git://github.com/overtrue/weather.git
创建时间: 2018-08-15T07:04:33Z
项目社区:https://github.com/overtrue/weather

开源协议:

关键词:
weather weather-api weather-sdk

下载


Weather

:rainbow: 基于高德开放平台的 PHP 天气信息组件。

Tests

Sponsor me

安装

  1. $ composer require overtrue/weather -vvv

配置

在使用本扩展之前,你需要去 高德开放平台 注册账号,然后创建应用,获取应用的 API Key。

使用

  1. use Overtrue\Weather\Weather;
  2. $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
  3. $weather = new Weather($key);

获取实时天气

  1. $response = $weather->getLiveWeather('深圳');

示例:

  1. {
  2. "status": "1",
  3. "count": "1",
  4. "info": "OK",
  5. "infocode": "10000",
  6. "lives": [
  7. {
  8. "province": "广东",
  9. "city": "深圳市",
  10. "adcode": "440300",
  11. "weather": "中雨",
  12. "temperature": "27",
  13. "winddirection": "西南",
  14. "windpower": "5",
  15. "humidity": "94",
  16. "reporttime": "2018-08-21 16:00:00"
  17. }
  18. ]
  19. }

获取近期天气预报

  1. $response = $weather->getForecastsWeather('深圳');

示例:

  1. {
  2. "status": "1",
  3. "count": "1",
  4. "info": "OK",
  5. "infocode": "10000",
  6. "forecasts": [
  7. {
  8. "city": "深圳市",
  9. "adcode": "440300",
  10. "province": "广东",
  11. "reporttime": "2018-08-21 11:00:00",
  12. "casts": [
  13. {
  14. "date": "2018-08-21",
  15. "week": "2",
  16. "dayweather": "雷阵雨",
  17. "nightweather": "雷阵雨",
  18. "daytemp": "31",
  19. "nighttemp": "26",
  20. "daywind": "无风向",
  21. "nightwind": "无风向",
  22. "daypower": "≤3",
  23. "nightpower": "≤3"
  24. },
  25. {
  26. "date": "2018-08-22",
  27. "week": "3",
  28. "dayweather": "雷阵雨",
  29. "nightweather": "雷阵雨",
  30. "daytemp": "32",
  31. "nighttemp": "27",
  32. "daywind": "无风向",
  33. "nightwind": "无风向",
  34. "daypower": "≤3",
  35. "nightpower": "≤3"
  36. },
  37. {
  38. "date": "2018-08-23",
  39. "week": "4",
  40. "dayweather": "雷阵雨",
  41. "nightweather": "雷阵雨",
  42. "daytemp": "32",
  43. "nighttemp": "26",
  44. "daywind": "无风向",
  45. "nightwind": "无风向",
  46. "daypower": "≤3",
  47. "nightpower": "≤3"
  48. },
  49. {
  50. "date": "2018-08-24",
  51. "week": "5",
  52. "dayweather": "雷阵雨",
  53. "nightweather": "雷阵雨",
  54. "daytemp": "31",
  55. "nighttemp": "26",
  56. "daywind": "无风向",
  57. "nightwind": "无风向",
  58. "daypower": "≤3",
  59. "nightpower": "≤3"
  60. }
  61. ]
  62. }
  63. ]
  64. }

获取 XML 格式返回值

以上两个方法第二个参数为返回值类型,可选 jsonxml,默认 json

  1. $response = $weather->getLiveWeather('深圳', 'xml');

示例:

  1. <response>
  2. <status>1</status>
  3. <count>1</count>
  4. <info>OK</info>
  5. <infocode>10000</infocode>
  6. <lives type="list">
  7. <live>
  8. <province>广东</province>
  9. <city>深圳市</city>
  10. <adcode>440300</adcode>
  11. <weather>中雨</weather>
  12. <temperature>27</temperature>
  13. <winddirection>西南</winddirection>
  14. <windpower>5</windpower>
  15. <humidity>94</humidity>
  16. <reporttime>2018-08-21 16:00:00</reporttime>
  17. </live>
  18. </lives>
  19. </response>

参数说明

  1. array | string getLiveWeather(string $city, string $format = 'json')
  2. array | string getForecastsWeather(string $city, string $format = 'json')
  • $city - 城市名/高德地址位置 adcode,比如:“深圳” 或者(adcode:440300);
  • $format - 输出的数据格式,默认为 json 格式,当 output 设置为 “xml” 时,输出的为 XML 格式的数据。

在 Laravel 中使用

在 Laravel 中使用也是同样的安装方式,配置写在 config/services.php 中:

  1. .
  2. .
  3. .
  4. 'weather' => [
  5. 'key' => env('WEATHER_API_KEY'),
  6. ],

然后在 .env 中配置 WEATHER_API_KEY

  1. WEATHER_API_KEY=xxxxxxxxxxxxxxxxxxxxx

可以用两种方式来获取 Overtrue\Weather\Weather 实例:

方法参数注入

  1. .
  2. .
  3. .
  4. public function edit(Weather $weather)
  5. {
  6. $response = $weather->getLiveWeather('深圳');
  7. }
  8. .
  9. .
  10. .

服务名访问

  1. .
  2. .
  3. .
  4. public function edit()
  5. {
  6. $response = app('weather')->getLiveWeather('深圳');
  7. }
  8. .
  9. .
  10. .

参考

:heart: Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 :heart:

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT