j-crowe/open-weather

为 Open Weather 提供一个非常简单的封装

v1.2.0 2016-03-15 12:36 UTC

This package is auto-updated.

Last update: 2024-09-06 02:27:39 UTC


README

Build Status

PHP 封装 Open Weather API

此库不依赖于 Laravel;然而,包括了一个 Laravel 服务提供者。

安装

使用 composer 安装

"require": {
    "j-crowe/open-weather": "1.2"
}

或通过 CLI

composer require j-crowe/open-weather 1.2

配置

需要3个配置项

首先,app_id - 一个名为 'OPENWEATHER_APPID' 的环境变量,必须在您的应用程序中设置

请在此处查看 如何获取

其他两个 - 基础 URL 和默认 Guzzle 选项。

请在此处查看 可能的选项

无 Laravel 使用

下面是示例初始化代码

<?php

include_once 'vendor/autoload.php';

use JCrowe\OpenWeather\OpenWeather;

// Note the configs that are required. Guzzle Opts are default options for Guzzle
$baseUrl = 'http://api.openweathermap.org';
$guzzleOpts = array(
    'timeout' => 3,
    'connect_timeout' => 3
);
$appId = 'abcdefghijklmnop1234567890';

$openWeather = OpenWeather::getInstance($guzzleOpts, $baseUrl, $appId);

$response = $openWeather->getByCityName('los angeles');

if($response->isValid()) {
    print_r($response->getWeatherData());
}

?>

与 Laravel 一起使用

config/app.php 中,将以下内容添加到服务提供者数组中。

array(
    ...
    'JCrowe\OpenWeather\Providers\OpenWeatherServiceProvider',
)

然后添加以下内容到别名数组中。

'OpenWeather' => 'JCrowe\OpenWeather\Facades\OpenWeather'

使用 php artisan config:publish j-crowe/open-weather 发布配置,应该包括默认配置。