Download the PHP package ctw/ctw-http without Composer
On this page you can find all versions of the php package ctw/ctw-http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package ctw-http
Short Description This package provides utility classes and constants to facilitate common operations of PSR-7 and a group of exceptions which represent HTTP status codes.
License BSD-3-Clause
Informations about the package ctw-http
Package "ctw/ctw-http"
Utility classes, constants, and typed exceptions for PSR-7 HTTP operations, providing a complete HTTP status code registry with metadata and throwable exceptions for all 4xx/5xx status codes.
Introduction
Why This Library Exists
Working with HTTP in PHP often involves scattered magic numbers, inconsistent error handling, and repetitive exception creation. The PSR-7 standard defines message interfaces but doesn't provide status code constants, metadata, or exception types.
This library provides a complete HTTP toolkit:
- Status code constants: All standard HTTP status codes as class constants
- Method constants: HTTP method names (GET, POST, PUT, DELETE, etc.)
- Status metadata: Human-readable names, descriptions, and documentation URLs
- Typed exceptions: Dedicated exception classes for every 4xx and 5xx status code
- Exception hierarchy: Catch client errors (4xx) or server errors (5xx) separately
Problems This Library Solves
- Magic numbers: Code uses
404instead of readableSTATUS_NOT_FOUND - Inconsistent exceptions: Each project invents its own HTTP exception classes
- Missing metadata: Status codes lack human-readable descriptions
- Catch-all handling: No way to catch only client errors vs. server errors
- Documentation gaps: Developers must look up status code meanings externally
Where to Use This Library
- PSR-7 applications: Enhance HTTP message handling with typed constants
- API development: Throw semantically correct HTTP exceptions
- Error handling middleware: Catch exceptions by category (client/server)
- Logging and monitoring: Include status metadata in error reports
- HTTP clients: Use constants instead of magic numbers
Design Goals
- PSR-7 aligned: Implements
Fig\Http\Message\StatusCodeInterface - Complete coverage: All standard HTTP status codes (1xx through 5xx)
- Rich metadata: Name, phrase, and documentation URL for each status
- Exception hierarchy: Abstract base classes for client (4xx) and server (5xx) errors
- Type-safe: Full strict types and interface contracts
Requirements
- PHP 8.3 or higher
- fig/http-message-util ^1.1
Installation
Install by adding the package as a Composer requirement:
Usage Examples
HTTP Status Metadata
Retrieve complete metadata for any status code:
Throwing HTTP Exceptions
Each 4xx and 5xx status code has a dedicated exception class:
Catching Exceptions by Category
The exception hierarchy allows catching by error category:
Exception Hierarchy
HTTP Method Constants
HTTP Status Constants
Available Exception Classes
| Status | Exception Class |
|---|---|
| 400 | BadRequestException |
| 401 | UnauthorizedException |
| 402 | PaymentRequiredException |
| 403 | ForbiddenException |
| 404 | NotFoundException |
| 405 | MethodNotAllowedException |
| 406 | NotAcceptableException |
| 407 | ProxyAuthenticationRequiredException |
| 408 | RequestTimeoutException |
| 409 | ConflictException |
| 410 | GoneException |
| 411 | LengthRequiredException |
| 412 | PreconditionFailedException |
| 413 | PayloadTooLargeException |
| 414 | RequestUriTooLongException |
| 415 | UnsupportedMediaTypeException |
| 416 | RangeNotSatisfiableException |
| 417 | ExpectationFailedException |
| 418 | ImATeapotException |
| 421 | MisdirectedRequestException |
| 422 | UnprocessableEntityException |
| 423 | LockedException |
| 424 | FailedDependencyException |
| 425 | TooEarlyException |
| 426 | UpgradeRequiredException |
| 428 | PreconditionRequiredException |
| 429 | TooManyRequestsException |
| 431 | RequestHeaderFieldsTooLargeException |
| 451 | UnavailableForLegalReasonsException |
| 500 | InternalServerErrorException |
| 501 | NotImplementedException |
| 502 | BadGatewayException |
| 503 | ServiceUnavailableException |
| 504 | GatewayTimeoutException |
| 505 | VersionNotSupportedException |
| 506 | VariantAlsoNegotiatesException |
| 507 | InsufficientStorageException |
| 508 | LoopDetectedException |
| 510 | NotExtendedException |
| 511 | NetworkAuthenticationRequiredException |