effectra / http-message
The Effectra HTTP Message package.
v1.0.3
2024-01-06 20:45 UTC
Requires
- php: ^8.0.2
- psr/http-message: ^1.1 || ^2.0
README
A lightweight PHP library for working with HTTP messages.
Installation
Install the library using Composer:
composer require effectra/http-message
Usage
Creating a Request
use Effectra\Http\Message\Request; use Effectra\Http\Message\Uri; use Effectra\Http\Message\Stream; // Create a request $uri = new Uri('https://api.example.com/users'); $body = new Stream(json_encode(['name' => 'John Doe'])); $request = new Request('POST', $uri, [], $body); // Get request information echo $request->getMethod(); // Output: POST echo $request->getUri(); // Output: https://api.example.com/users echo $request->getBody()->getContents(); // Output: {"name":"John Doe"}
Creating a Response
use Effectra\Http\Message\Response; use Effectra\Http\Message\Stream; // Create a response $body = new Stream('Hello, world!'); $response = new Response(200, [], $body); // Get response information echo $response->getStatusCode(); // Output: 200 echo $response->getReasonPhrase(); // Output: OK echo $response->getBody()->getContents(); // Output: Hello, world!
Creating a Stream
use Effectra\Http\Message\Stream; // Create a stream from a string $stream = new Stream('Hello, world!'); // Read from the stream echo $stream->getContents(); // Output: Hello, world! // Write to the stream $stream->write('Goodbye!'); echo $stream->getContents(); // Output: Goodbye!
Working with Uploaded Files
use Effectra\Http\Message\UploadedFile; // Create an uploaded file instance $file = $_FILES['my_file']; $uploadedFile = new UploadedFile( $file['tmp_name'], $file['size'], $file['error'], $file['name'], $file['type'] ); // Get information about the uploaded file echo $uploadedFile->getClientFilename(); // Output: my_file.txt echo $uploadedFile->getClientMediaType(); // Output: text/plain // Move the uploaded file to a target location $targetPath = '/path/to/destination'; $uploadedFile->moveTo($targetPath);
Working with URIs
use Effectra\Http\Message\Uri; // Create a URI instance $uri = new Uri('https://example.com/path?query=param#fragment'); // Get individual URI components echo $uri->getScheme(); // Output: https echo $uri->getHost(); // Output: example.com echo $uri->getPath(); // Output: /path echo $uri->getQuery(); // Output: query=param echo $uri->getFragment(); // Output: fragment // Modify the URI components $modifiedUri = $uri ->withScheme('http') ->withHost('example.org') ->withPath('/new-path') ->withQuery('key=value') ->withFragment('new-fragment'); echo $modifiedUri; // Output: http://example.org/new-path?key=value#new-fragment
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.
License
This library is released under the MIT License. See LICENSE for more information.