loomlabs / http-component
A component for handling HTTP Requests/Responses
1.0.1
2025-05-17 20:28 UTC
Requires
- ext-curl: *
- psr/http-client: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- loomlabs/badger: ^1.0
- phpunit/phpunit: ^12.1
README
The HTTP Component is a PHP library designed to simplify the process of making HTTP requests and handling HTTP responses in your applications. It follows the [PSR-7 HTTP Message Interface](https://www.php-fig.org/psr/psr-7/) standards for HTTP messages, making it compatible with other libraries and frameworks that also adhere to these standards.
Installation
You can install this library using Composer:
composer require loomlabs/http-component
Features
This HTTP Component package provides the following key features:
Request and Response Handling
Request
andResponse
classes that implement thePsr\Http\Message\RequestInterface
andPsr\Http\Message\ResponseInterface
, respectively.- Easily create and manipulate HTTP requests and responses.
- Handle headers, request methods, status codes, and more.
Stream Handling
- A
Stream
class that implements thePsr\Http\Message\StreamInterface
for working with stream data. - Read and write data to streams, check for stream availability, and more.
HTTP Client
- A
HttpClient
class that implements thePsr\Http\Client\ClientInterface
. - Simplifies sending HTTP requests using cURL and processing HTTP responses.
- Supports common HTTP methods like GET, POST, PUT, PATCH and DELETE.
- Automatically parses response headers and handles redirects.
URI Handling
- A
Uri
class that implements thePsr\Http\Message\UriInterface
for working with URIs. - Easily construct and manipulate URIs, including handling scheme, host, port, path, query, and fragment.
Usage
Creating an HTTP Request
use Loom\HttpComponent\HttpClient;
use Loom\HttpComponent\Request;
use Loom\HttpComponent\StreamBuilder;
use Loom\HttpComponent\Uri;
// Create a URI
$uri = new Uri('https', 'example.com', '/');
// Create an HTTP GET request
$body = 'Some text!';
$request = new Request(
'GET',
$uri,
['Content-Type' => 'application/json'],
StreamBuilder::build($body)
);
// Customise the request headers
$request = $request->withHeader('Authorization', 'Bearer AccessToken');
// Send the request using the built-in HTTP Client
$response = (new HttpClient())->sendRequest($request);
// Get the response status code
$status = $response->getStatusCode();
// Get the response body
$body = $response->getBody()->getContents();
Creating an HTTP Client
use Loom\HttpComponent\HttpClient;
// Create an HTTP client
$client = new HttpClient();
// Send GET request
$response = $client->get('https://example.com/api/resource');
// Send POST request to endpoint with headers and body
$response = $client->post(
'https://example.com/api/resource',
['Content-Type' => 'application/json', 'Authorization' => 'Bearer AccessToken'],
json_encode(['data' => 'value'])
);
Working with Streams
use Loom\HttpComponent\StreamBuilder;
// Create a stream from a string
$stream = StreamBuilder::build('Hello, World!');
// Read from the stream
$data = $stream->read(1024);
// Write to the stream
$stream->write('New data to append');
// Rewind the streams internal pointer
$stream->rewind();
// Get the stream contents
$contents = $stream->getContents();
URI Handling
use Loom\HttpComponent\Uri;
use Loom\HttpComponent\Web\WebServerUri;
// Create a URI
$uri = new Uri('https', 'example.com', '/api/resource');
// Modify the URI
$uri = $uri->withScheme('http');
$uri = $uri->withPort(8888);
$uri = $uri->withQuery('new_param=new_value');
// Get the URI as a string
$uriString = $uri->__toString();
// Build a URI based on the current request to your web server
$uri = WebServerUri::generate();
License
This package is open-source software licensed under the GNU General Public License, version 3.0 (GPL-3.0).