tal7aouy / yusr
A lightweight HTTP client library for PHP inspired by Guzzle.
v1.0.1
2024-10-13 15:01 UTC
Requires
- php: >=7.4 || ^8.0
- ext-curl: *
- ext-json: *
- psr/http-client: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- laravel/pint: ^1.18
- mockery/mockery: ^1.6
- pestphp/pest: ^3.3
This package is auto-updated.
Last update: 2024-10-18 08:46:49 UTC
README
YusrClient is a robust and easy-to-use PHP HTTP client that simplifies making HTTP requests in your PHP applications. It implements the PSR-18 HTTP Client interface and provides a fluent API for sending HTTP requests.
🌟 Features
- 🔒 Implements PSR-18 HTTP Client interface
- 🛠 Singleton pattern for easy global access
- 🚦 Supports all major HTTP methods (GET, POST, PUT, DELETE, PATCH)
- 🔧 Customizable options for each request
- 🧩 Easy-to-use fluent interface
- 🔁 Automatic handling of redirects
- ⏱ Configurable timeout
- 🔐 SSL verification
📦 Installation
You can install the package via composer:
composer require tal7aouy/yusr
🚀 Usage
Here's a quick example of how to use YusrClient:
use Yusr\Http\YusrClient; // Get the YusrClient instance $client = YusrClient::getInstance(); // Make a GET request $response = $client->get('https://jsonplaceholder.typicode.com/posts'); // Access response data $statusCode = $response->getStatusCode(); $body = $response->getBody()->getContents(); $headers = $response->getHeaders(); echo $body. PHP_EOL;
🛠 Configuration
You can configure the default options when getting the YusrClient instance:
$client = YusrClient::getInstance([ 'timeout' => 60, 'allow_redirects' => false, 'verify' => false, ]);
📘 API Reference
Available Methods
get(string $uri, array $options = []): ResponseInterface
post(string $uri, array $options = []): ResponseInterface
put(string $uri, array $options = []): ResponseInterface
delete(string $uri, array $options = []): ResponseInterface
patch(string $uri, array $options = []): ResponseInterface
request(string $method, string $uri, array $options = []): ResponseInterface
Options
query
: array of query parameters to add to the URIheaders
: array of headers to send with the requestbody
: the body of the request (for POST, PUT, PATCH)timeout
: request timeout in secondsallow_redirects
: whether to follow redirectsverify
: whether to verify SSL certificates
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is open-sourced software licensed under the MIT license.