PHPackages                             psr-discovery/http-factory-implementations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. psr-discovery/http-factory-implementations

ActiveLibrary[HTTP &amp; Networking](/categories/http)

psr-discovery/http-factory-implementations
==========================================

Lightweight library that discovers available PSR-17 HTTP Factory implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.

1.2.0(1y ago)78.4M—6.8%3[1 PRs](https://github.com/psr-discovery/http-factory-implementations/pulls)14MITPHPPHP ^8.2

Since Mar 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/psr-discovery/http-factory-implementations)[ Packagist](https://packagist.org/packages/psr-discovery/http-factory-implementations)[ Docs](https://github.com/psr-discovery/http-factory-implementations)[ RSS](/packages/psr-discovery-http-factory-implementations/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (5)Dependencies (10)Versions (6)Used By (14)

**Lightweight library that discovers available [PSR-17 HTTP Factory](https://www.php-fig.org/psr/psr-17/) implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.**

This package is part of the [PSR Discovery](https://github.com/psr-discovery) utility suite, which also supports [PSR-18 HTTP Clients](https://github.com/psr-discovery/http-client-implementations), [PSR-14 Event Dispatchers](https://github.com/psr-discovery/event-dispatcher-implementations), [PSR-11 Containers](https://github.com/psr-discovery/container-implementations), [PSR-6 Caches](https://github.com/psr-discovery/cache-implementations) and [PSR-3 Logs](https://github.com/psr-discovery/log-implementations).

This is largely intended for inclusion in libraries like SDKs that wish to support PSR-17 Factories without requiring hard dependencies on specific implementations or demanding extra configuration by users.

- [Requirements](#requirements)
- [Implementations](#implementations)
- [Installation](#installation)
- [Usage](#usage)
- [Handling Failures](#handling-failures)
- [Exceptions](#exceptions)
- [Singletons](#singletons)
- [Mocking Priority](#mocking-priority)
- [Preferring an Implementation](#preferring-an-implementation)
- [Using a Specific Implementation](#using-a-specific-implementation)

Requirements
------------

[](#requirements)

- PHP 8.2+
- Composer 2.0+

Successful discovery requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.

Implementations
---------------

[](#implementations)

The following `psr/http-factory-implementation` implementations are discovered and instantiated automatically:

- [guzzlehttp/psr7](https://github.com/guzzle/psr7) ^2.0
- [http-interop/http-factory-guzzle](https://github.com/http-interop/http-factory-guzzle) ^0.2 | ^1.0
- [httpsoft/http-message](https://github.com/httpsoft/http-message) ^1.0.4
- [laminas/laminas-diactoros](https://github.com/laminas/laminas-diactoros) ^2.0
- [nimbly/capsule](https://github.com/nimbly/Capsule) ^2.0
- [nyholm/psr7](https://github.com/Nyholm/psr7) ^0.2.2 | ^1.0
- [slim/psr7](https://github.com/slimphp/Slim-Psr7) ^1.0
- [tuupola/http-factory](https://github.com/tuupola/http-factory) ^1.0.2
- [typo3/core](https://github.com/TYPO3-CMS/core) ^10.1 | ^11.0 | ^12.0
- [zendframework/zend-diactoros](https://github.com/zendframework/zend-diactoros) ^2.0

The following mock implementations are also available:

- [psr-mock/http-factory-implementation](https://github.com/psr-mock/http-factory-implementation) ^1.0

If [a particular implementation](https://packagist.org/providers/psr/http-factory-implementation) is missing that you'd like to see, please open a pull request adding support.

Installation
------------

[](#installation)

```
composer require --dev psr-discovery/http-factory-implementations
```

Usage
-----

[](#usage)

```
use PsrDiscovery\Discover;

// Returns a PSR-17 RequestFactoryInterface instance
$requestFactory = Discover::httpRequestFactory();

// Returns a PSR-17 ResponseFactoryInterface instance
$responseFactory = Discover::httpResponseFactory();

// Returns a PSR-17 StreamFactoryInterface instance
$streamFactory = Discover::httpStreamFactory();

// Returns a PSR-17 UploadedFileFactoryInterface instance
$uploadedFileFactory = Discover::httpUploadedFileFactory();

// Returns a PSR-17 UriFactoryInterface instance
$uriFactory = Discover::httpUriFactory();

// Returns a PSR-7 RequestInterface instance
$request = $requestFactory->createRequest('GET', 'https://example.com');
```

Handling Failures
-----------------

[](#handling-failures)

If the library is unable to discover a suitable PSR-17 implementation, the `Discover::httpRequestFactory()`, `Discover::httpResponseFactory()` or `Discover::httpStreamFactory()` discovery methods will simply return `null`. This allows you to handle the failure gracefully, for example by falling back to a default implementation.

Example:

```
use PsrDiscovery\Discover;

$requestFactory = Discover::httpRequestFactory();

if ($requestFactory === null) {
    // No suitable HTTP RequestFactory implementation was discovered.
    // Fall back to a default implementation.
    $requestFactory = new DefaultRequestFactory();
}
```

Singletons
----------

[](#singletons)

By default, the `Discover::httpRequestFactory()`, `Discover::httpResponseFactory()` or `Discover::httpStreamFactory()` methods will always return a new instance of the discovered implementation. If you wish to use a singleton instance instead, simply pass `true` to the `$singleton` parameter of the discovery method.

Example:

```
use PsrDiscovery\Discover;

// $httpResponseFactory1 !== $httpResponseFactory2 (default)
$httpResponseFactory1 = Discover::httpResponseFactory();
$httpResponseFactory2 = Discover::httpResponseFactory();

// $httpResponseFactory1 === $httpResponseFactory2
$httpResponseFactory1 = Discover::httpResponseFactory(singleton: true);
$httpResponseFactory2 = Discover::httpResponseFactory(singleton: true);
```

Mocking Priority
----------------

[](#mocking-priority)

This library will give priority to searching for a known, available mocking library before searching for a real implementation. This is to allow for easier testing of code that uses this library.

The expectation is that these mocking libraries will always be installed as development dependencies, and therefore if they are available, they are intended to be used.

Preferring an Implementation
----------------------------

[](#preferring-an-implementation)

If you wish to prefer a specific implementation over others, you can `prefer()` it by package name:

```
use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\RequestFactories;

// Prefer the a specific implementation of PSR-17 over others.
RequestFactories::prefer('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$factory = Discover::httpRequestFactory();
```

In this case, this will cause the `httpRequestFactory()` method to return the preferred implementation if it is available, otherwise, it will fall back to the default behavior. The same applies to `httpResponseFactory()` and `httpStreamFactory()` when their relevant classes are configured similarly.

Note that assigning a preferred implementation will give it priority over the default preference of mocking libraries.

Using a Specific Implementation
-------------------------------

[](#using-a-specific-implementation)

If you wish to force a specific implementation and ignore the rest of the discovery candidates, you can `use()` its package name:

```
use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\ResponseFactories;

// Only discover a specific implementation of PSR-17.
ResponseFactories::use('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or null if it is not available.
$factory = Discover::httpResponseFactory();
```

In this case, this will cause the `httpResponseFactory()` method to return the preferred implementation if it is available, otherwise, it will return `null`. The same applies to `httpRequestFactory()` and `httpStreamFactory()` when their relevant classes are configured similarly.

---

This library is not produced or endorsed by, or otherwise affiliated with, the PHP-FIG.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity52

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 93.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~124 days

Recently: every ~147 days

Total

6

Last Release

529d ago

PHP version history (3 changes)1.0.0PHP ^8.0

1.1.1PHP ^8.1

1.2.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3093?v=4)[Evan Sims](/maintainers/evansims)[@evansims](https://github.com/evansims)

---

Top Contributors

[![evansims](https://avatars.githubusercontent.com/u/3093?v=4)](https://github.com/evansims "evansims (29 commits)")[![flavioheleno](https://avatars.githubusercontent.com/u/471860?v=4)](https://github.com/flavioheleno "flavioheleno (1 commits)")[![stephenfrank](https://avatars.githubusercontent.com/u/112059?v=4)](https://github.com/stephenfrank "stephenfrank (1 commits)")

---

Tags

discoveryhttp-factoryhttp-factory-implementationpsrpsr-17psrpsr-18discovery

###  Code Quality

TestsPest

Static AnalysisPHPStan, Psalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/psr-discovery-http-factory-implementations/health.svg)

```
[![Health](https://phpackages.com/badges/psr-discovery-http-factory-implementations/health.svg)](https://phpackages.com/packages/psr-discovery-http-factory-implementations)
```

###  Alternatives

[psr/http-client

Common interface for HTTP clients

1.7k680.7M2.1k](/packages/psr-http-client)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[vultr/vultr-php

The Official Vultr API PHP Wrapper.

2243.9k1](/packages/vultr-vultr-php)[art4/requests-psr18-adapter

Use WordPress/Requests as a PSR-18 HTTP client

153.3k](/packages/art4-requests-psr18-adapter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
