adachsoft / web-content-fetch-contract
Provider-agnostic SPI contracts and exceptions for fetching and extracting web content from URLs in PHP.
Package info
gitlab.com/a.adach/web-content-fetch-contract
pkg:composer/adachsoft/web-content-fetch-contract
Requires
- php: >=8.3
Requires (Dev)
- adachsoft/php-code-style: ^0.7.1
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^11.0 || ^12.0
- rector/rector: ^2.6
- symplify/phpstan-rules: ^14.9
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A provider-agnostic PHP contract for fetching and extracting textual content from web pages.
Purpose
This package defines a stable service provider interface (SPI) that keeps application code independent from the HTTP client, browser automation tool, DOM parser, readability engine, or AI reader used to retrieve web content.
Features
- A small
WebContentFetcherInterfacefor URL-based content extraction. - Provider-specific options without coupling consumers to a concrete implementation.
- A dedicated exception hierarchy for invalid URLs, unsupported options, and fetch failures.
- PSR-4 autoloading and no runtime dependencies beyond PHP.
Installation
composer require adachsoft/web-content-fetch-contract
Integration
Implement the interface in an adapter package and inject that adapter into the application service that needs web content:
use AdachSoft\WebContentFetchContract\WebContentFetcherInterface;
final class ArticleReader
{
public function __construct(
private readonly WebContentFetcherInterface $contentFetcher,
) {
}
public function read(string $url): string
{
return $this->contentFetcher->fetchContent($url);
}
}
Provider-specific options can be passed as an associative array:
$content = $contentFetcher->fetchContent(
'https://example.com/article',
['timeout' => 10],
);
Catch ContentFetchExceptionInterface when the application needs one handler for all contract-level failures, or catch the specialized exceptions for more granular error handling.
See docs/examples/basic-usage.md for a complete adapter example.