run6/weather

天气 SDK

0.0.1 2019-06-10 06:22 UTC

This package is auto-updated.

Last update: 2024-09-11 17:50:35 UTC


README

天气 SDK。

Build Status StyleCI Scrutinizer Code Quality Packagist

安装

$ composer require run6/weather -vvv

使用

配置

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

开始

use Run6\Weather\Weather;

$key = 'xxxxxxxxxxxxxxx'; //替换成你的API—KEY

$weather = new Weather($key);

获取实时天气

$response = $weather->getWeather('杭州');

返回实例

{
  "status": "1",
  "count": "1",
  "info": "OK",
  "infocode": "10000",
  "lives": [
    {
      "province": "浙江",
      "city": "杭州市",
      "adcode": "330100",
      "weather": "多云",
      "temperature": "33",
      "winddirection": "",
      "windpower": "≤3",
      "humidity": "50",
      "reporttime": "2019-06-10 13:51:19"
    }
  ]
}

获取近期天气预报

$response = $weather->getWeather('杭州','all')

返回实例

{
  "status": "1",
  "count": "1",
  "info": "OK",
  "infocode": "10000",
  "forecasts": [
    {
      "city": "杭州市",
      "adcode": "330100",
      "province": "浙江",
      "reporttime": "2019-06-10 13:51:19",
      "casts": [
        {
          "date": "2019-06-10",
          "week": "1",
          "dayweather": "小雨",
          "nightweather": "多云",
          "daytemp": "33",
          "nighttemp": "21",
          "daywind": "东北",
          "nightwind": "东北",
          "daypower": "≤3",
          "nightpower": "≤3"
        },
        {
          "date": "2019-06-11",
          "week": "2",
          "dayweather": "多云",
          "nightweather": "多云",
          "daytemp": "29",
          "nighttemp": "21",
          "daywind": "东北",
          "nightwind": "东北",
          "daypower": "≤3",
          "nightpower": "≤3"
        },
        {
          "date": "2019-06-12",
          "week": "3",
          "dayweather": "多云",
          "nightweather": "多云",
          "daytemp": "29",
          "nighttemp": "21",
          "daywind": "",
          "nightwind": "",
          "daypower": "4",
          "nightpower": "4"
        },
        {
          "date": "2019-06-13",
          "week": "4",
          "dayweather": "小雨",
          "nightweather": "",
          "daytemp": "29",
          "nighttemp": "18",
          "daywind": "无风向",
          "nightwind": "无风向",
          "daypower": "≤3",
          "nightpower": "≤3"
        }
      ]
    }
  ]
}

获取 XML 格式返回值

第三个参数为返回值类型,可选 jsonxml ,默认 json

$response = $weather('杭州','base','xml')

返回实例

<response>
  <status>1</status>
  <count>1</count>
  <info>OK</info>
  <infocode>10000</infocode>
  <lives type="list">
    <live>
      <province>浙江</province>
      <city>杭州市</city>
      <adcode>330100</adcode>
      <weather>多云</weather>
      <temperature>33</temperature>
      <winddirection>东</winddirection>
      <windpower>≤3</windpower>
      <humidity>50</humidity>
      <reporttime>2019-06-10 13:51:19</reporttime>
    </live>
  </lives>
</response>

参数说明

array|string getWeather(string $city, string $type = 'base', string $format = 'json')
  • $city - 城市名,比如:"杭州";
  • $type - 返回内容:base: 返回实时天气 / all:返回天气预报
  • $format - 输出的数据格式,默认为 json, 当设置为 xml 时,输出 XML 格式数据。

Laravel 中使用

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

    .
    .
    .
    'weather' => [
        'key' => env('WEATHER_API_KEY'),
    ]   

然后在 .env 文件中配置 WEATHER_API_KEY

WEATHER_API_KEY=xxxxxxxxx

可以使用两种方式获取 Run6\Weather\Weather 实例:

方式参数注入

    ·
    ·
    ·
    // 在控制器中通过依赖注入的方式
    public function getWeather(Weather $weather)
    {
        $response = $weather->getWeather('杭州');
    }
    .
    .
    .

服务名访问

    .
    .
    .
    pubcli function getWeather()
    {
        $response = app('weather')->getWeather('杭州');
    }
    .
    .
    .

参考

贡献

你可以通过以下三种方式之一进行贡献:

  1. 使用 问题追踪器 提交错误报告。
  2. 问题追踪器 上回答问题或修复错误。
  3. 贡献新功能或更新 wiki。

代码贡献过程并不正式。你只需确保遵循 PSR-0、PSR-1 和 PSR-2 编码指南。任何新的代码贡献都必须附带适用的情况下的单元测试。

许可证

MIT