chubes4/ai-http-client
最新稳定版本:v2.0.13
Composer 安装命令:
composer require chubes4/ai-http-client
包简介
A professional WordPress library for unified AI provider communication. Supports OpenAI, Anthropic, Google Gemini, Grok, and OpenRouter with standardized request/response formats.
关键字:
README 文档
README
Archived: This package is superseded by WordPress core's
wp-ai-clientAPIs. Data Machine no longer uses this runtime dependency; provider/model/key administration and image generation have moved towp-ai-client-shaped capabilities. This repository remains available for historical reference only and should not be used for new work.
A professional WordPress library for unified AI provider communication. Supports OpenAI, Anthropic, Google Gemini, Grok, and OpenRouter with standardized request/response formats.
Key Features:
- WordPress filter-based architecture with self-contained provider classes
- Unified request/response format across all AI providers
- Comprehensive caching system with 24-hour model cache TTL
- Multi-modal support (text, images, files) via native Files API integration
- Streaming and standard request modes with proper error handling
- REST API endpoints for configuration and management
- Multisite network-wide API key storage support
- Comprehensive error handling with WordPress action hooks
Installation
Composer (recommended for standalone use):
composer require chubes4/ai-http-client
Git Subtree (recommended for plugin embedding):
git subtree add --prefix=lib/ai-http-client https://github.com/chubes4/ai-http-client.git main --squash
Manual Installation: Download and include in your WordPress plugin or theme
Requirements: PHP 7.4+, WordPress environment
Upgrading from v1.x to v2.0
Breaking Changes: All filter/action hooks renamed from ai_* to chubes_ai_* for WordPress.org compliance.
Automatic Migration
API keys are automatically migrated on first admin page load:
- Old option:
ai_http_shared_api_keys - New option:
chubes_ai_http_shared_api_keys - Old option deleted after 30 days
Hook Migration
Update all references in your code:
// OLD (v1.x) apply_filters('ai_providers', []) apply_filters('ai_provider_api_keys', null) apply_filters('ai_models', $provider) apply_filters('ai_tools', []) apply_filters('ai_request', $request) apply_filters('ai_file_to_base64', '', $path) apply_filters('ai_http', [], $method, $url, $args) // NEW (v2.0) apply_filters('chubes_ai_providers', []) apply_filters('chubes_ai_provider_api_keys', null) apply_filters('chubes_ai_models', $provider) apply_filters('chubes_ai_tools', []) apply_filters('chubes_ai_request', $request) apply_filters('chubes_ai_file_to_base64', '', $path) apply_filters('chubes_ai_http', [], $method, $url, $args)
See docs/CHANGELOG.md for complete migration details.
Usage
Include Library:
// Composer: Auto-loads via Composer (no includes needed) // Git Subtree/Manual: Include in your plugin require_once plugin_dir_path(__FILE__) . 'lib/ai-http-client/ai-http-client.php';
Basic Request:
$response = apply_filters('chubes_ai_request', [ 'messages' => [['role' => 'user', 'content' => 'Hello AI!']] ], 'openai'); // Provider name is now required
Advanced Options:
// Specific provider (required parameter) $response = apply_filters('chubes_ai_request', $request, 'anthropic'); // With streaming callback $response = apply_filters('chubes_ai_request', $request, 'openai', $streaming_callback); // With function calling tools $response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools); // With conversation continuation $response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools, $conversation_data);
Providers
Comprehensive AI provider support with dynamic model discovery:
- OpenAI - GPT models, OpenAI Responses API, streaming, function calling, native Files API integration
- Anthropic - Claude models, streaming, function calling, native Files API integration with vision support
- Google Gemini - Gemini models, streaming, function calling, native Files API integration with vision support
- Grok/X.AI - Grok models, streaming support
- OpenRouter - 200+ models via unified API gateway
Architecture
- Filter-Based: WordPress-native provider registration via
chubes_ai_providersfilter - Self-Contained: Each provider handles format conversion internally (standard ↔ provider format)
- Unified Interface: All providers accept standard format, return normalized responses
- WordPress-Native: Uses wp_remote_* for HTTP, WordPress transients for caching
- Modular Design: Provider files self-register, no central coordination needed
- Error Handling: Comprehensive error hook via
chubes_ai_library_erroraction - Performance: 24-hour model caching with granular cache clearing
Multi-Plugin Support
- Plugin-isolated configurations via filter-based settings
- Centralized API key storage in
chubes_ai_http_shared_api_keysoption - Multisite network-wide API key storage support
- No provider conflicts through self-contained architecture
- Independent AI settings per consuming plugin
Core Components
- Providers: Self-contained classes with unified interface (OpenAI, Anthropic, Gemini, Grok, OpenRouter)
- Request Processing: Complete pipeline via
chubes_ai_requestfilter with error handling - HTTP Layer: Centralized
chubes_ai_httpfilter supporting streaming and standard requests - Caching System: Model caching via
AIHttpCacheclass with WordPress transients - REST API: Configuration and management endpoints via
ai_http_clientnamespace - Error Management: Centralized logging via
AIHttpErrorclass
Core Filters
// Provider Discovery $providers = apply_filters('chubes_ai_providers', []); // API Keys Management $keys = apply_filters('chubes_ai_provider_api_keys', null); // Get all keys apply_filters('chubes_ai_provider_api_keys', $new_keys); // Update all keys // Dynamic Model Fetching (with 24-hour cache) $models = apply_filters('chubes_ai_models', $provider_name, $config); // AI Tools Registration $tools = apply_filters('chubes_ai_tools', []); // File Operations $base64 = apply_filters('chubes_ai_file_to_base64', '', $file_path, $options); // HTTP Requests (internal use) $result = apply_filters('chubes_ai_http', [], 'POST', $url, $args, 'Context');
REST API Endpoints
The library provides REST API endpoints for configuration and management:
// Configure API keys via REST API wp_remote_post('/wp-json/ai-http-client/v1/api-keys/openai', [ 'body' => wp_json_encode([ 'api_key' => 'your-api-key' ]), 'headers' => [ 'Content-Type' => 'application/json', 'X-WP-Nonce' => wp_create_nonce('wp_rest') ] ]); // Get provider configuration $config = wp_remote_get('/wp-json/ai-http-client/v1/api-keys/openai', [ 'headers' => [ 'X-WP-Nonce' => wp_create_nonce('wp_rest') ] ]); // Get available models for a provider $models = wp_remote_get('/wp-json/ai-http-client/v1/models/openai', [ 'headers' => [ 'X-WP-Nonce' => wp_create_nonce('wp_rest') ] ]); // Get all available providers $providers = wp_remote_get('/wp-json/ai-http-client/v1/providers', [ 'headers' => [ 'X-WP-Nonce' => wp_create_nonce('wp_rest') ] ]);
Available Endpoints:
- GET/POST
/wp-json/ai-http-client/v1/api-keys/{provider}- Get/set API key for specific provider - GET
/wp-json/ai-http-client/v1/models/{provider}- Get available models for a provider - GET
/wp-json/ai-http-client/v1/providers- List all available providers
Multi-Plugin Configuration
Shared API Keys Storage:
// WordPress option: 'chubes_ai_http_shared_api_keys' $shared_keys = apply_filters('chubes_ai_provider_api_keys', null); // Returns: ['openai' => 'sk-...', 'anthropic' => 'sk-ant-...', ...]
Provider Configuration:
// Each provider accepts configuration in constructor $provider = new AI_HTTP_OpenAI_Provider([ 'api_key' => 'sk-...', 'organization' => 'org-...', 'base_url' => 'https://api.openai.com/v1' // Optional custom endpoint ]);
AI Tools System
Tool Registration:
add_filter('chubes_ai_tools', function($tools) { $tools['file_processor'] = [ 'class' => 'FileProcessor_Tool', 'category' => 'file_handling', 'description' => 'Process files and extract content', 'parameters' => [ 'file_path' => [ 'type' => 'string', 'required' => true, 'description' => 'Path to file to process' ] ] ]; return $tools; });
Tool Discovery and Usage:
// Get all registered tools $all_tools = apply_filters('chubes_ai_tools', []); // Pass tools to AI request $response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools); // Note: Tool execution is handled by consuming plugins
Distribution
- Packagist: Available via
composer require chubes4/ai-http-client - GitHub: https://github.com/chubes4/ai-http-client
- Version: 2.0.7 - Professional WordPress library with BaseProvider architecture
- License: GNU GPL v3
- Dependencies: None (pure WordPress integration)
- Multi-plugin: Safe for concurrent use by multiple WordPress plugins
Adding Providers
class AI_HTTP_MyProvider { public function __construct($config = []) { /* Provider setup */ } public function is_configured() { /* Check if ready */ } public function request($standard_request) { /* Standard → Provider → Standard */ } public function streaming_request($standard_request, $callback) { /* Streaming support */ } public function get_normalized_models() { /* Get models for UI */ } public function get_raw_models() { /* Get raw API response */ } } // Self-register via filter add_filter('chubes_ai_providers', function($providers) { $providers['myprovider'] = [ 'class' => 'AI_HTTP_MyProvider', 'type' => 'llm', 'name' => 'My Provider' ]; return $providers; });
Version 2.0.0 Features
WordPress.org Compliance:
- All filter/action hooks renamed from
ai_*tochubes_ai_*prefix - Automatic migration system for API keys and settings
- Backward compatibility migration for 30-day rollback window
Core Architecture:
- WordPress filter-based provider registration with self-contained classes
- Unified request/response format across all providers
- Comprehensive caching system with 24-hour model cache TTL
- Native Files API integration for multi-modal content (text, images, files)
- Streaming and standard request modes with proper error handling
- REST API endpoints for configuration and management
- Multisite network-wide API key storage support
AI Provider Support:
- OpenAI Responses API integration with native Files API support
- Anthropic Claude models with dynamic model discovery and native Files API
- Google Gemini with native Files API and multi-modal support
- Grok/X.AI integration with streaming support
- OpenRouter gateway access to 200+ models
WordPress Integration:
- Native WordPress HTTP API usage with centralized
chubes_ai_httpfilter - WordPress transients for model caching with granular cache clearing
- WordPress options API for settings with multisite support
- Comprehensive error handling via
chubes_ai_library_erroraction hook
Production Usage
This library is actively used in production WordPress plugins:
- Data Machine - AI-powered content processing pipelines with multi-provider support
- WordSurf - AI content editor with streaming responses and function calling
- AI Bot for bbPress - Forum AI responses with contextual conversation management
Debug
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
Debug Logging Covers:
- HTTP request/response cycles via
chubes_ai_httpfilter - Provider-specific API interactions
- Model caching operations and cache hits/misses
- Streaming request handling
- Error conditions via
chubes_ai_library_erroraction hook - File upload operations to provider APIs
Contributing
Pull requests welcome for:
- Additional AI provider integrations
- Performance optimizations and caching improvements
- WordPress compatibility enhancements
- Template component additions
- Documentation improvements
License
GNU GPL v3 - Chris Huber
chubes4/ai-http-client 适用场景与选型建议
chubes4/ai-http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.47k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「ai」 「http-client」 「artificial-intelligence」 「Gemini」 「grok」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chubes4/ai-http-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chubes4/ai-http-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chubes4/ai-http-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Mock PSR-18 HTTP client
Common interface for HTTP clients
A Laravel Nova tool to ask ChatGPT and store question with answer to db to be viewed.
An easy to use wrapper for the curl http library in PHP
Simple builder for the sending request based on the component symfony/http-client
🚀 GigaChat PHP SDK - полнофункциональная библиотека для интеграции с Sber GigaChat API. Простое подключение GigaChat к PHP-приложениям с поддержкой Laravel. Генерация текста, чат-боты, AI-ассистенты. Полная документация, примеры кода, OAuth авторизация.
统计信息
- 总下载量: 2.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-09-14