ohrionmartin/weather
Composer 安装命令:
composer require ohrionmartin/weather
包简介
Reusable Slim components for fetching weather via OpenWeather One Call API 3.0
README 文档
README
To integrate weather data into your application using the ohrionmartin/weather package, follow these steps:
1. Add the Repository and Dependency
Update your composer.json file to include the custom repository and require the ohrionmartin/weather package:
"repositories": [
{
"type": "vcs",
"url": "https://git.nampharm.com.na/ohrionmartin/weather"
}
],
"require": {
"ohrionmartin/weather": "dev-master"
}
Then run:
composer update
2. Configure the API Key
Ensure you have an API key from OpenWeatherMap. Add it to your .env file:
OPENWEATHER_API_KEY=your_api_key_here
3. Add Weather Route
In your application (e.g., in routes.php or equivalent), add the following route to handle weather requests:
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Ohrionmartin\Weather\Service\OpenWeatherClient;
$router->get('/weather', function (Request $request) {
$apiKey = env('OPENWEATHER_API_KEY');
if (!$apiKey) {
return response()->json(['error' => 'OPENWEATHER_API_KEY is not configured'], 500);
}
$client = new Client();
$ow = new OpenWeatherClient($client, $apiKey);
$options = [];
if ($request->has('units')) {
$options['units'] = $request->get('units'); // 'standard'|'metric'|'imperial'
}
if ($request->has('lang')) {
$options['lang'] = $request->get('lang');
}
if ($request->has('exclude')) {
$options['exclude'] = $request->get('exclude'); // comma-separated segments
}
try {
if ($request->has('city')) {
$data = $ow->oneCallByCity((string) $request->get('city'), $options);
} else {
if (!$request->has('lat') || !$request->has('lon')) {
return response()->json(['error' => 'Provide either ?city=Name or ?lat=..&lon=..'], 400);
}
$lat = (float) $request->get('lat');
$lon = (float) $request->get('lon');
$data = $ow->oneCall($lat, $lon, $options);
}
return response()->json($data);
} catch (\RuntimeException $e) {
return response()->json(['error' => $e->getMessage()], 502);
}
});
4. Usage Examples
You can access the weather endpoint with the following query parameters:
- By city name:
http://localhost:8080/weather?city=London - By coordinates:
http://localhost:8080/weather?lat=51.5074&lon=-0.1278 - With optional parameters:
units:standard,metric, orimperial(e.g.,?units=metric)lang: Language code for response (e.g.,?lang=fr)exclude: Comma-separated segments to exclude (e.g.,?exclude=minutely,hourly)
Example request:
curl "http://localhost:8080/weather?city=London&units=metric&lang=fr"
This will return weather data in JSON format for London in metric units and French language.
ohrionmartin/weather 适用场景与选型建议
ohrionmartin/weather 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ohrionmartin/weather 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ohrionmartin/weather 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-05