jinchun/php-garmin-connect-api
Composer 安装命令:
composer require jinchun/php-garmin-connect-api
包简介
PHP library to connect and use garmin wellness api
关键字:
README 文档
README
Description
PHP library to connect and use garmin wellness api
Version Support
This library now supports both Chinese and International versions of Garmin Connect API:
- International Version (default): Uses
connectapi.garmin.comandhealthapi.garmin.com - Chinese Version: Uses
connectapi.garmin.cn(OAuth) andgcs-wellness.garmin.cn(Health REST API)
Version Usage Examples
Creating API instances for different versions
use Stoufa\GarminApi\GarminApi; $config = [ 'identifier' => getenv('GARMIN_KEY'), 'secret' => getenv('GARMIN_SECRET'), 'callback_uri' => getenv('GARMIN_CALLBACK_URI') ]; // International version (default) $internationalApi = new GarminApi($config, GarminApi::VERSION_INTERNATIONAL); // Chinese version $chineseApi = new GarminApi($config, GarminApi::VERSION_CHINESE);
Dynamic version switching
$api = new GarminApi($config); // Switch to Chinese version $api->useChineseVersion(); // Switch to International version $api->useInternationalVersion(); // Get current configuration echo "Current version: " . $api->getVersion(); echo "API URL: " . $api->getApiUrl(); echo "User API URL: " . $api->getUserApiUrl(); echo "Auth URL: " . $api->getAuthUrl();
Version-specific endpoints
-
Chinese Version:
- OAuth API URL:
https://connectapi.garmin.cn/(for request/access token) - Health REST API URL:
https://gcs-wellness.garmin.cn/wellness-api/rest/(for activities and user data) - Auth URL:
http://connect.garmin.cn/oauthConfirm - Tools URL:
https://healthtools.garmin.cn/tools/login(Data Viewer / Backfill / Ping)
- OAuth API URL:
-
International Version:
- API URL:
https://connectapi.garmin.com/ - User API URL:
https://healthapi.garmin.com/wellness-api/rest/ - Auth URL:
http://connect.garmin.com/oauthConfirm
- API URL:
Backward Compatibility
Existing code will continue to work without any changes, as the international version is used by default.
HTTP Client Configuration
This library supports custom HTTP client configuration to handle various network scenarios, including proxy settings, timeouts, SSL verification, and custom headers.
Configuration Options
You can configure the HTTP client using any of the following Guzzle HTTP client options:
timeout- Request timeout in seconds (default: 30)connect_timeout- Connection timeout in seconds (default: 10)proxy- Proxy server configurationverify- SSL certificate verification (default: true)headers- Default headers to apply to all requestsallow_redirects- Controls redirect behaviorcookies- Cookie jar configurationdebug- Enable debug output
Usage Examples
1. Configure via constructor
use Stoufa\GarminApi\GarminApi; $config = [ 'identifier' => getenv('GARMIN_KEY'), 'secret' => getenv('GARMIN_SECRET'), 'callback_uri' => getenv('GARMIN_CALLBACK_URI') ]; $httpConfig = [ 'timeout' => 60, 'connect_timeout' => 15, 'proxy' => 'http://proxy.example.com:8080', 'verify' => false, 'headers' => [ 'User-Agent' => 'My Custom App/1.0' ] ]; $server = new GarminApi($config, GarminApi::VERSION_INTERNATIONAL, $httpConfig);
2. Configure using setter method
$server = new GarminApi($config); $server->setHttpClientConfig([ 'timeout' => 45, 'proxy' => 'http://proxy.example.com:8080', 'headers' => [ 'User-Agent' => 'My Custom App/1.0' ] ]);
3. Get current configuration
$currentConfig = $server->getHttpClientConfig(); print_r($currentConfig);
4. Common use cases
Set custom timeout for slow networks
$server->setHttpClientConfig([ 'timeout' => 120, 'connect_timeout' => 30 ]);
Configure proxy server
$server->setHttpClientConfig([ 'proxy' => 'http://username:password@proxy.example.com:8080' ]);
Disable SSL verification (for development)
$server->setHttpClientConfig([ 'verify' => false ]);
Set custom User-Agent
$server->setHttpClientConfig([ 'headers' => [ 'User-Agent' => 'MyApp/1.0 (contact@example.com)' ] ]);
Enable debug mode
$server->setHttpClientConfig([ 'debug' => true ]);
All HTTP requests made by the library (OAuth authentication, API calls, etc.) will use the custom configuration.
Installtion
composer require jinchun/php-garmin-connect-api
Example
Please take a look at examples folder for complete usage examples, including version switching examples.
Usage
Get authorization link
use Stoufa\GarminApi\GarminApi; try { $config = array( 'identifier' => getenv('GARMIN_KEY'), 'secret' => getenv('GARMIN_SECRET'), 'callback_uri' => getenv('GARMIN_CALLBACK_URI') ); $server = new GarminApi($config); // Retreive temporary credentials from server $temporaryCredentials = $server->getTemporaryCredentials(); // Save temporary crendentials in session to use later to retreive authorization token $_SESSION['temporaryCredentials'] = $temporaryCredentials; // Get authorization link $link = $server->getAuthorizationUrl($temporaryCredentials); } catch (\Throwable $th) { // catch your exception here }
Get token credentials
After the user connects his garmin account successfully it will redirect to callback_uri. "oauth_token" and "oauth_verifier" should be available in $_GET.
try { $config = array( 'identifier' => getenv('GARMIN_KEY'), 'secret' => getenv('GARMIN_SECRET'), 'callback_uri' => getenv('GARMIN_CALLBACK_URI') ); $server = new GarminApi($config); // Retrieve the temporary credentials we saved before $temporaryCredentials = $_SESSION['temporaryCredentials']; // We will now retrieve token credentials from the server. $tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']); } catch (\Throwable $th) { // catch your exception here }
Get Garmin user id
$userId = $server->getUserUid($tokenCredentials);
Backfill activities
When you connect garmin account and get token credentials first time, you won't be able to get previous activities because garmin does not give you activities older than your token credentials. Instead you need to use backfill method to fullfull your token with previous activities (no more than one month).
// backfill activities for last 7 days ago $params = [ 'summaryStartTimeInSeconds' => strtotime("-7 days", time()), 'summaryEndTimeInSeconds' => time() ]; $server->backfillActivitySummary($tokenCredentials, $params);
Deregistration
$server->deleteUserAccessToken($tokenCredentials);
Avalaible methods (for the moment)
Get summary activities
$params = [ 'uploadStartTimeInSeconds' => 1598814036, // time in seconds utc 'uploadEndTimeInSeconds' => 1598900435 // time in seconds utc ]; // Activity summaries $server->getActivitySummary($tokenCredentials, $params); // Manually activity summaries $server->getManuallyActivitySummary($tokenCredentials, $params); // Activity details summaries $server->getActivityDetailsSummary($tokenCredentials, $params); // User metrics (including VO2 max and fitness age) $server->getUserMetrics($tokenCredentials, $params);
Backfill activities
// For backfill params can be with upload start time $params = [ 'uploadStartTimeInSeconds' => 1598814036, // time in seconds utc 'uploadEndTimeInSeconds' => 1598900435 // time in seconds utc ]; // or with summary start time $params = [ 'summaryStartTimeInSeconds' => 1598814036, // time in seconds utc 'summaryEndTimeInSeconds' => 1598900435 // time in seconds utc ]; // Backfill activity summaries $server->backfillActivitySummary($tokenCredentials, $params); // Backfill daily activity summaries $server->backfillDailySummary($tokenCredentials, $params); // Backfill epoch summaries $server->backfillEpochSummary($tokenCredentials, $params); // Backfill activity details summaries $server->backfillActivityDetailsSummary($tokenCredentials, $params); // Backfill sleep summaries $server->backfillSleepSummary($tokenCredentials, $params); // Backfill body composition summaries $server->backfillBodyCompositionSummary($tokenCredentials, $params); // Backfill stress details summaries $server->backfillStressDetailsSummary($tokenCredentials, $params); // Backfill user metrics summaries $server->backfillUserMetricsSummary($tokenCredentials, $params); // Backfill pulse ox summaries $server->backfillPulseOxSummary($tokenCredentials, $params); // Backfill respiration summaries $server->backfillRespirationSummary($tokenCredentials, $params);
Get User Metrics (VO2 Max)
The getUserMetrics method retrieves user fitness metrics including VO2 max and fitness age. This method requires both uploadStartTimeInSeconds and uploadEndTimeInSeconds parameters with a maximum 24-hour window.
// Get user metrics for a specific 24-hour period $params = [ 'uploadStartTimeInSeconds' => 1726876800, // Unix timestamp 'uploadEndTimeInSeconds' => 1726963200 // Unix timestamp (max 24 hours later) ]; $userMetrics = $server->getUserMetrics($tokenCredentials, $params); // The response will include VO2 max data and other fitness metrics // Example response structure: // { // "userMetrics": [ // { // "vo2Max": 45.2, // "fitnessAge": 28, // "timestamp": "2023-09-21T12:00:00Z" // } // ] // }
Important: The time window for user metrics queries cannot exceed 24 hours.
jinchun/php-garmin-connect-api 适用场景与选型建议
jinchun/php-garmin-connect-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「garmin」 「garmin-connect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jinchun/php-garmin-connect-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jinchun/php-garmin-connect-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jinchun/php-garmin-connect-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library to connect and use garmin wellness api
A PSR-7 compatible library for making CRUD API endpoints
PHP library for parsing cycling GPS activities and calculating metrics.
Garmin API PHP client with OAuth authentication
Alfabank REST API integration
Tools to view and parse Garmin FIT files.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-22