lakondev / simple-api-service
There is no license information available for the latest version (v1.0.0) of this package.
Generic API service to simplify HTTP requests in Laravel
v1.0.0
2025-05-04 07:50 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.0
README
A lightweight, Guzzle-based HTTP API wrapper for Laravel. Simplifies making API calls without having to write boilerplate code for curl, headers, or token handling.
Installation
You can install the package via Composer:
composer require lakondev/simple-api-service
Usage
1. Initialize the Service
// Import the service class use SimpleApiService\SimpleApiService; // Create a new instance of the service $api = new SimpleApiService();
2. Configuration
// Define the configuration array with base_url and optional token $config = [ 'base_url' => 'https://jsonplaceholder.typicode.com', // Base URL of the API 'token' => null, // Optional token (set if required by the API) ];
3. GET Request
// Create the complete URL for the specific endpoint $url = $api->getApiUrl($config, 'todos/1'); // Prepare request options, including headers $options = $api->getRequestOptions($config); // Execute GET request to the API $response = $api->executeRequest('GET', $url, $options); // Output the API response dd($response);
4. POST Request
// Define the endpoint URL $url = $api->getApiUrl($config, 'posts'); // Define request body to send in POST request $body = [ 'json' => [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1, ] ]; // Merge headers with request body $options = $api->getRequestOptions($config, $body); // Send POST request $response = $api->executeRequest('POST', $url, $options); // Output the response dd($response);
Requirements
- PHP >= 8.0
- Laravel 9+
- guzzlehttp/guzzle >= 7.0
License
This package is open-sourced software licensed under the MIT license.