https://github.com/kielabokkie/laravel-guzzle-api-service
A base class for interacting with external APIs via Guzzle
https://github.com/kielabokkie/laravel-guzzle-api-service
Last synced: 3 months ago
JSON representation
A base class for interacting with external APIs via Guzzle
- Host: GitHub
- URL: https://github.com/kielabokkie/laravel-guzzle-api-service
- Owner: kielabokkie
- Created: 2019-09-30T04:54:51.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-28T02:30:37.000Z (over 5 years ago)
- Last Synced: 2025-01-19T17:11:28.005Z (5 months ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Guzzle API Service
**Note: This package is still being developed and is not production ready. Use at your own risk!**
## Installation
Install the package via composer:
composer require kielabokkie/laravel-guzzle-api-service
## Package configuration
Publish the config file by running the following command:
```bash
php artisan vendor:publish --provider="Kielabokkie\GuzzleApiService\GuzzleApiServiceProvider"
```This is the contents of the file that will be published at `config/api-service.php`:
```php
return [
/*
* Enable logging of request and responses to storage/logs/api-service.log
*/
'logging_enabled' => env('API_SERVICE_LOGGING_ENABLED', false),/*
* The namespace where your API Service classes are created under.
* This will be appended to your base namespace. So the config below
* will create a class under App\Support\Services.
*/
'namespace' => 'Support\Services'
];
```## Setup
To make use of the base API Client class you'll need to add the required `$baseUrl` to set the base URL of your API. You'll also have to call the `$this->setClient();` function in the constructor of your service class.
```php
setClient();
}
}
```To make it easy to get started you can use the following command to scaffold your API Service class:
```bash
php artisan make:api-service HttpBinService
```This will create a class called `HttpBinService.php` in the `app/Support/Services` folder. All you have to do is set your `$baseUrl` and you are good to go.
Note: If you would like your classes to be placed somewhere else you can overwrite the `namespace` variable in the `api-service.php` config file.
## Usage
### Get request
Now to execute a `GET` request you can simply do the following:
```php
public function yourGetRequest()
{
$response = $this->get('/get'));return json_decode($response->getBody()->getContents());
}
```This is pretty basic stuff and the same as you would normally do a `GET` request with Guzzle.
### Adding default headers
APIs often require you to add a specific header to every request, for example for authorisation purposes. Instead of having to pass that as an option with every request you can add the following function at the top of your service class:
```php
protected function defaultHeaders()
{
return [
'Authorization' => 'Bearer abcdef123456',
];
}
```### Adding default query parameters
You can add default query parameters to every request automatically in a similar way:
```php
protected function defaultQueryParams()
{
return [
'token' => 'your-token'
];
}
```This will automatically append the token as a get parameter like so: `https://httpbin.org/get?token=your-token`.