Skip to content

Commit c16f9b7

Browse files
author
artisan
committed
first commit
0 parents  commit c16f9b7

13 files changed

Lines changed: 508 additions & 0 deletions

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.DS_Store
2+
/vendor
3+
/coverage
4+
sftp-config.json
5+
composer.lock
6+
.subsplit
7+
.php_cs.cache

.php_cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the overtrue/weather.
4+
5+
(c) overtrue <i@overtrue.me>
6+
7+
This source file is subject to the MIT license that is bundled.
8+
EOF;
9+
10+
return PhpCsFixer\Config::create()
11+
->setRiskyAllowed(true)
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'header_comment' => array('header' => $header),
15+
'array_syntax' => array('syntax' => 'short'),
16+
'ordered_imports' => true,
17+
'no_useless_else' => true,
18+
'no_useless_return' => true,
19+
'php_unit_construct' => true,
20+
'php_unit_strict' => true,
21+
))
22+
->setFinder(
23+
PhpCsFixer\Finder::create()
24+
->exclude('vendor')
25+
->in(__DIR__)
26+
)
27+
;

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "overtrue/weather",
3+
"description": "A weather SDK.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "overtrue",
8+
"email": "i@overtrue.me"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"Overtrue\\Weather\\": "./src"
14+
}
15+
},
16+
"require": {
17+
"guzzlehttp/guzzle": "^6.3",
18+
"phpunit/phpunit": "^7.3",
19+
"mockery/mockery": "^1.1"
20+
}
21+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/Exceptions/Exception.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/weather.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Overtrue\Weather\Exceptions;
12+
13+
/**
14+
* Class Exception.
15+
*/
16+
class Exception extends \Exception
17+
{
18+
}

src/Exceptions/HttpException.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/weather.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Overtrue\Weather\Exceptions;
12+
13+
/**
14+
* Class HttpException.
15+
*/
16+
class HttpException extends Exception
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/weather.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Overtrue\Weather\Exceptions;
12+
13+
/**
14+
* Class InvalidArgumentException.
15+
*/
16+
class InvalidArgumentException extends Exception
17+
{
18+
}

0 commit comments

Comments
 (0)