konradmichalik / php-outbound
Outbound - Declarative API clients for PHP: describe external APIs as interfaces with attributes, with retries, caching, rate limits and safe defaults.
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- ext-mbstring: *
- ext-tokenizer: *
- cuyz/valinor: ^2.7
- psr/log: ^2.0 || ^3.0
- symfony/console: ^7.4
- symfony/dependency-injection: ^7.4
- symfony/http-client: ^7.4
- symfony/http-client-contracts: ^3.5
- symfony/http-foundation: ^7.4
- symfony/mime: ^7.4
- symfony/rate-limiter: ^7.4.23
- symfony/yaml: ^7.4
Requires (Dev)
- armin/editorconfig-cli: ^2.0
- ergebnis/composer-normalize: ^2.42
- ergebnis/phpstan-rules: ^2.13
- konradmichalik/php-cs-fixer-preset: ^0.3.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.5 || ^12.0
- psr/container: ^2.0
- rector/rector: ^2.0
- shipmonk/composer-dependency-analyser: ^1.8
- spaze/phpstan-disallowed-calls: ^4.0
- symfony/config: ^7.4
- tomasvotruba/cognitive-complexity: ^1.0
- tomasvotruba/type-coverage: ^2.5
Suggests
- phpunit/phpunit: Required for OutboundTestTrait.
Provides
None
Conflicts
None
Replaces
None
README
php-outbound
Declarative API clients for PHP: describe external APIs as interfaces with attributes and get the implementation generated. Your code calls methods and receives typed objects, without HTTP code of its own.
Note
The goal is to replace the hand-written service wrapper per API: you declare what an API offers, and timeouts, retries, caching and security come as safe defaults.
Outbound as in outgoing requests: your application calls the API.
Warning
This package is at an early stage. Attributes and APIs may still change before 1.0.
✨ Features
- Less code per API: one interface method per endpoint and typed DTOs instead of request classes and HTTP plumbing
- Fast: cached responses, revalidation with ETags and a miss lock
- Keeps working when an API fails: short timeouts, safe retries, a circuit breaker and, if you allow it, stale data to bridge an outage
- Secure by default: HTTPS only, allowed hosts, private networks blocked, secrets from the environment and redacted logs
- Quick start from OpenAPI: interfaces and DTOs generated from an OpenAPI document or a sample response
- Tests without network: mocks by endpoint instead of URL
🔥 Installation
Requires PHP 8.2+ and Symfony components 7.4.
composer require konradmichalik/php-outbound
Using TYPO3? Install konradmichalik/typo3-outbound, which builds on this package.
🚀 Quick start
Declare the API as an interface carrying #[Connection] and annotate a method with an HTTP verb:
use KonradMichalik\Outbound\Attribute as Outbound; #[Outbound\Connection(baseUri: 'https://api.weather.example.com')] interface WeatherApi { /** @return array{city: string, temperature: float} */ #[Outbound\Get('/forecast/{city}')] public function forecast(string $city): array; }
Compose the runtime and call the method:
use KonradMichalik\Outbound\Outbound; $weather = Outbound::create([WeatherApi::class])->api(WeatherApi::class); $temperature = $weather->forecast('Dresden')['temperature'];
forecast('Dresden') sends GET https://api.weather.example.com/forecast/Dresden, checks the JSON response against the declared shape and returns it. For larger responses, return a DTO class instead of an array shape.
Everything else is opt-in. A larger API splits into resources, takes its base URI and secret from the runtime configuration, maps responses to DTOs, retries, caches and maps a 404 to null, all declared with attributes:
use KonradMichalik\Outbound\Value\{Secret, Setting}; #[Outbound\Connection(baseUri: new Setting('crm.baseUri'), allowedHosts: ['crm.example.com'])] #[Outbound\Auth\Bearer(token: new Secret('crm.token'))] #[Outbound\Retry(maxRetries: 2)] interface CrmApi {} #[Outbound\Resource('/contacts')] interface ContactsApi extends CrmApi { #[Outbound\Get('/{id}')] #[Outbound\Cache(lifetime: 600, tags: ['crm_contact_{id}'], staleOnError: 3600)] #[Outbound\OnStatus(404, returns: null)] public function find(int $id): ?ContactDto; }
See Defining endpoints for connections, resources, parameters and return types, and Features for every opt-in attribute.
📚 Documentation
Full documentation lives in docs/: defining endpoints, features, plain PHP usage and background.
🧑💻 Contributing
Please have a look at CONTRIBUTING.md.
⭐ License
This project is licensed under the MIT License.