flores / request-manager
A simple HTTP request manager using cURL
Installs: 41
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/flores/request-manager
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^10.0
README
A PHP library for managing HTTP requests using cURL.
📞 Installation
You can install this library via Composer:
composer require flores/request-manager
🚀 Usage
Creating a Request
use Flores\RequestManager\Requests\GetRequest; $request = new GetRequest('https://yoururl.com/api/resource'); $response = $request->send(); echo $response->getDataString();
Setting Headers
$request->setHeader('Authorization', 'Bearer token') ->setHeader('Accept', 'application/json');
Setting Authentication
$request->setBearerToken('your_token_here'); // Uses Bearer Token authentication $request->setAuth('username', 'password'); // Uses Basic Auth
Sending JSON Data (POST)
use Flores\RequestManager\Requests\PostRequest; $request = new PostRequest('https://yoururl.com/api/resource'); $request->setJsonBody([ 'title' => 'New Post', 'body' => 'Post content', 'userId' => 1 ]); $response = $request->send(); echo $response->getData();
Retrieving Response Data
echo $response->getData(); // Returns the processed response (object) echo $response->getDataString(); // Returns the processed response as JSON String
Getting the Raw cURL Response
If you need the raw response from the cURL request, use getResponse():
echo $response->get(); // Returns the raw response as a string
Available Methods
Request Methods
setUrl($url): Sets the request URL.setHeader($key, $value): Sets an HTTP header.setHeaders(array $headers): Sets multiple headers at once.setPayloads(array $payloads): Sets the request body.setJsonBody(array $data): Sets the request body as JSON.setBearerToken($token): Sets the Authorization header using Bearer Token.setAuth($user, $passwd): Sets Basic Authentication credentials.setContentType($contentType): Sets the request content type.setCurlOption($key, $value): Sets a custom cURL option.getUrl(): Returns the request URL.getPayloads(): Returns the data sent in the request.getContentType(): Returns the defined content type.send(): Executes the request and returns anHttpResponseobject.
Response Methods
get(): Returns the raw cURL response.getData(): Returns the structured response (object).getDataString(): Returns the response in JSON format.
Example of a DELETE Request
use Flores\RequestManager\Requests\DeleteRequest; $request = new DeleteRequest('https://yoururl.com/api/resource/1'); $response = $request->send(); echo $response->getDataString();
Error Handling
try { $request = new PostRequest('https://yoururl.com/api/resource'); $request->setPayloads(['title' => 'Error Test']); $response = $request->send(); echo $response->getData(); } catch (HttpRequestException $e) { echo "Request error: " . $e->getMessage(); } catch (HttpConnectionException $e) { echo "Connection error: " . $e->getMessage(); }
⚠️ Handled Exceptions
InvalidContentTypeException: Invalid content type.HttpRequestException: HTTP request error.HttpConnectionException: Server connection failure.
📄 License
This project is licensed under the MIT License.