surfoo/geocaching-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

surfoo/geocaching-php-sdk

Composer 安装命令:

composer require surfoo/geocaching-php-sdk

包简介

Geocaching PHP SDK

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version

The documentation about the API is available here:

You can follow changes about the documentation and the API here:

Requirements

Composer

From the command line:

composer require surfoo/geocaching-php-sdk

Using the PHP SDK

First, you must have your API key from Groundspeak, but access to the API are no longer open.

You can find an example of implementation (with OAuth 2) in this repository: https://github.com/Surfoo/geocaching-api-demo

Documentation

Start with docs/README.md for getting started, authentication, configuration, reliability patterns, and domain-by-domain API examples.

HTTP Request/Response Logging

The SDK provides comprehensive HTTP logging capabilities to help with debugging and monitoring API interactions. The logging feature includes:

  • Request/Response Correlation: Each request gets a unique ID to match requests with their responses
  • Automatic Token Masking: Sensitive authentication tokens are automatically masked for security
  • Configurable Detail Levels: Choose what information to log (headers, bodies, timing)
  • Status-based Log Levels: Automatic log level adjustment based on HTTP status codes
  • Performance Metrics: Request duration tracking in milliseconds

Quick Setup

use Geocaching\Enum\Environment;
use Geocaching\GeocachingSdk;
use Geocaching\Options;
use Psr\Log\LogLevel;

// Initialize SDK options
$options = new Options([
    'environment' => Environment::STAGING,
    'access_token' => 'your_token_here',
]);

// Enable HTTP logging (automatically configured with proper PSR-3 support)
$options->enableHttpLogging(
    'php://stdout',    // Output destination (console, file path, etc.)
    LogLevel::INFO,    // Log level (DEBUG, INFO, WARNING, ERROR)
    false,            // Log request/response bodies (true/false)
    true,             // Mask sensitive tokens (true/false)
    1000              // Max body length before truncation
);

// Create SDK instance
$sdk = new GeocachingSdk($options);

// All API calls will now be logged
$response = $sdk->ping();

// Optional: Enable automatic token refresh
use League\OAuth2\Client\Provider\Geocaching as OAuthProvider;

$oauthProvider = new OAuthProvider([
    'clientId'     => 'your_client_id',
    'clientSecret' => 'your_client_secret',
    'redirectUri'  => 'https://your-app.com/callback',
    'environment'  => 'production',
]);

$options->enableTokenRefresh([
    'reference_code' => 'PR12345',           // User reference code
    'storage'        => $tokenStorage,        // TokenStorageInterface implementation
    'oauth_provider' => $oauthProvider,
]);

// Or let the SDK create the OAuth provider automatically:
$options->enableTokenRefreshWithCredentials([
    'reference_code' => 'PR12345',
    'storage'        => $tokenStorage,
    'client_id'      => 'your_client_id',
    'client_secret'  => 'your_client_secret',
    'redirect_uri'   => 'https://your-app.com/callback',
    'environment'    => 'production',
]);

Configuration Options

Simple Logging (enableHttpLogging)

Parameter Type Default Description
$output string 'php://stdout' Output destination (console, file path, etc.)
$level string LogLevel::INFO Base log level for successful requests
$logBodies bool false Whether to include request/response bodies
$maskTokens bool true Automatically mask sensitive authentication data
$maxBodyLength int 1000 Maximum body length before truncation
$logFormat string|null null Custom log format (null for default)

Advanced Logging (enableHttpLoggingWithLogger)

Parameter Type Default Description
$logger LoggerInterface required Custom PSR-3 compatible logger
$level string LogLevel::INFO Base log level for successful requests
$logBodies bool false Whether to include request/response bodies
$maskTokens bool true Automatically mask sensitive authentication data
$maxBodyLength int 1000 Maximum body length before truncation

Log Output Example

[2025-01-15 10:30:15] geocaching-api.INFO: [GEOCACHING] HTTP Request: GET https://staging.api.groundspeak.com/v1/users/PR12345 {"request_id":"req_65a5b2f7c8d9e","method":"GET","uri":"https://staging.api.groundspeak.com/v1/users/PR12345","headers":{"authorization":["***"],"content-type":["application/json"]}}

[2025-01-15 10:30:15] geocaching-api.INFO: [GEOCACHING] HTTP Response: 200 OK (245.67ms) {"request_id":"req_65a5b2f7c8d9e","status_code":200,"duration_ms":245.67,"reason_phrase":"OK","headers":{"content-type":["application/json"],"x-ratelimit-remaining":["999"]}}

Security Features

  • Token Masking: Automatically masks tokens in URLs (access_token=***) and headers (Authorization: ***)
  • Selective Header Logging: Only logs relevant response headers, excluding sensitive information
  • Body Truncation: Large request/response bodies are truncated to prevent log flooding

Advanced Usage

Using a Custom Logger

For full control over logging behavior:

use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Processor\UidProcessor;

// Create custom logger with rotating files and unique request IDs
$customLogger = new Logger('geocaching-api');
$customLogger->pushHandler(new RotatingFileHandler('logs/geocaching.log', 7));
$customLogger->pushProcessor(new UidProcessor());

// Use custom logger
$options->enableHttpLoggingWithLogger(
    $customLogger,
    LogLevel::DEBUG,
    true,  // Log bodies
    true,  // Mask tokens
    2000   // Max body length
);

Custom Log Format

You can customize the log format:

$options->enableHttpLogging(
    'logs/api.log',
    LogLevel::INFO,
    false,
    true,
    1000,
    "[%datetime%] %level_name%: %message%\n" // Custom format
);

Multiple Output Destinations

// Log to console for development
$options->enableHttpLogging('php://stdout', LogLevel::DEBUG);

// Or log to file for production
$options->enableHttpLogging('/var/log/geocaching-api.log', LogLevel::WARNING);

Contributing

All contributions are welcome! Feel free to submit pull requests, issues or ideas :-)

统计信息

  • 总下载量: 3.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 7
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固