How I've done it is by creating a Trait called GuzzleTrait. It looks like this:
<?php
namespace App\Library;
use GuzzleHttp;
trait GuzzleTrait {
/**
* @param $url
* @return GuzzleHttp\Message\FutureResponse|GuzzleHttp\Message\ResponseInterface|GuzzleHttp\Ring\Future\FutureInterface|null
* Setup function for the guzzle requests going out to the live subscription site
*/
private function guzzleRequest($url)
{
$client = new GuzzleHttp\Client();
$subResponse = $client->get(env('API').$url,[
'headers' => [
'Content-Type' => 'application/json',
'customer_name' => 'test',
'customer_key' => 's324ghe7cf77f652ef2f030b9f26'
]]);
return $subResponse;
}
private function GuzzlePostRequest($data, $url)
{
$client = new GuzzleHttp\Client();
$client->setDefaultOption('headers', [
'Content-Type' => 'application/json',
'customer_name' => 'test',
'customer_key' => 's324ghe7cf77f652ef2f030b9f26'
]);
$subResponse = $client->post(env('API').$url,['form_params' => $data]);
return $subResponse;
}
private function GuzzlePutRequest($data, $url)
{
$client = new GuzzleHttp\Client();
$client->setDefaultOption('headers', [
'Content-Type' => 'application/json',
'customer_name' => 'test',
'customer_key' => 's324ghe7cf77f652ef2f030b9f26'
]);
$subResponse = $client->put(env('API').$url,['body' => $data]);
return $subResponse;
}
}
I can then use that trait on the controller and make calls like this:
$subResponse = $this->guzzleRequest("/orders/$order->id");
Obviously you can modify the trait to return what you need it to. In my case, I was working with subscriptions, so this made sense.
Hope that helps.