定制 gautammkgarg/psr-for-wordpress 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gautammkgarg/psr-for-wordpress

Composer 安装命令:

composer require gautammkgarg/psr-for-wordpress

包简介

PSR implementations for WordPress that bridge PSR interfaces to native WordPress APIs. PSR-18 HTTP Client (wp_remote_request) and PSR-16 Simple Cache (transients / wp_cache) with companion key pattern.

README 文档

README

License: GPL v3

A PSR implementation library for WordPress that bridges PSR interfaces to WordPress's native APIs. Currently implements:

  • PSR-18 HTTP Client — uses wp_remote_request() with fallback to WpOrg\Requests
  • PSR-16 Simple Cache — uses set_transient() / wp_cache_set() with the companion key pattern

Why This Package Exists

WordPress bundles its own HTTP transport (wp_remote_request). It handles proxies, SSL, redirects, and other platform-specific concerns automatically.

Libraries like omnipay/common, and others use php-http/discovery to auto-detect a PSR-18 HTTP client. Without this package, they'd pull in Guzzle — adding a heavy dependency and bypassing WordPress's HTTP layer.

This package gives you:

  • WordPress HTTP API as the transport inside WordPress
  • WpOrg\Requests as fallback outside WordPress
  • Zero-argument constructor for php-http/discovery auto-detection
  • PSR-18 compliance — HTTP Client wherever ClientInterface is expected
  • PSR-16 compliance — Simple Cache wherever CacheInterface is expected

Requirements

  • PHP 8.1+
  • rmccue/requests: ^2.0
  • WordPress 5.0+ when used inside WordPress

Installation

composer require gautammkgarg/psr-for-wordpress

Usage

Auto-discovery (Omnipay, php-http/discovery)

Add to your project's composer.json:

{
    "extra": {
        "discovery": {
            "psr/http-client-implementation": "GautamMKGarg\\PsrForWordPress\\Http\\Psr18Client"
        }
    }
}

That's it. php-http/discovery will find Psr18Client automatically when any library calls Psr18ClientDiscovery::find().

Manual Usage

use GautamMKGarg\PsrForWordPress\Http\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new Psr18Client();
$factory = new Psr17Factory();

$request = $factory->createRequest('GET', 'https://api.example.com/endpoint');
$response = $client->sendRequest($request);

echo $response->getStatusCode();    // 200
echo (string) $response->getBody(); // JSON response body

Custom Options

$client = new Psr18Client([
    'timeout'     => 30,   // seconds (default: 10)
    'redirection' => 0,    // disable redirects
    'sslverify'   => false, // disable SSL verification (not recommended in production)
]);

Global Timeout Override via WordPress Filter

Instead of manually configuring the client, use WordPress's native hook:

add_filter('http_request_args', function(array $args, string $url): array {
    // Increase timeout for payment gateway API calls
    $args['timeout'] = 30;
    return $args;
}, 10, 2);

How It Works

Inside WordPress

When wp_remote_request() is available, the client calls it directly:

PSR-7 Request → Build WP args → wp_remote_request() → Parse WP response → PSR-7 Response
                                        ↓ on WP_Error
                               throw NetworkException

Outside WordPress

Install dependency using composer require rmccue/requests.

When wp_remote_request() is not available:

PSR-7 Request → Build Requests options → WpOrg\Requests\Requests::request() → Parse response → PSR-7 Response
                                                    ↓ on exception
                                          throw NetworkException

Exceptions

Exception Interface When Thrown
NetworkException Psr\Http\Client\NetworkExceptionInterface WP_Error or transport exception
RequestException Psr\Http\Client\RequestExceptionInterface Invalid request (e.g., empty URI)

Both exceptions implement getRequest() to retrieve the original PSR-7 request.

PSR-16 Simple Cache

Two adapters are provided, both implementing Psr\SimpleCache\CacheInterface:

Adapter WordPress Backend Persistence Best For
WpTransientsCache set_transient, get_transient Always persistent (DB by default, Redis if plugin) Tokens, API responses, settings that must survive requests
WpObjectCache wp_cache_set, wp_cache_get Only with Redis/Memcached plugin Query results, computed values reused within request

Usage

use GautamMKGarg\PsrForWordPress\Cache\WpTransientsCache;
use GautamMKGarg\PsrForWordPress\Cache\WpObjectCache;

// Persistent cache (survives requests)
$cache = new WpTransientsCache();

// In-memory cache (request-only unless Redis plugin)
$cache = new WpObjectCache('myplugin');

$cache->set('users', $users, 3600); // seconds
$users = $cache->get('users');
$cache->delete('users');

Companion Key Pattern — Storing false Correctly

WordPress returns false for both "key not found" and "value is false". We solve this by creating a companion key ({key}__found_psr16__) only when false is stored. Non-false values are stored raw — so native get_transient() can read them without mangling.

Feature This Package
False handling Companion key (only for false)
Native WordPress interop for non-false values ✅ Yes — values stored raw
Performance overhead Near-zero (companion keys created only for false values)

Guzzle vs This Package — Timeout Difference

Guzzle uses 0 as the default timeout, meaning no timeout at all. That's why Guzzle never fails for slow APIs. This package defaults to 10 seconds — which is more appropriate for production WordPress sites than WordPress's built-in 5-second default. Increase it as needed.

License

GPL-3.0-or-later. See LICENSE for full text.

gautammkgarg/psr-for-wordpress 适用场景与选型建议

gautammkgarg/psr-for-wordpress 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「cache」 「wordpress」 「requests」 「psr」 「wp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gautammkgarg/psr-for-wordpress 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gautammkgarg/psr-for-wordpress 我们能提供哪些服务?
定制开发 / 二次开发

基于 gautammkgarg/psr-for-wordpress 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 44
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2026-04-15