Definitely, for any PHP project, you can use GuzzleHTTP to send requests. Guzzle has some very good documentation that you can check here . I just want to say that you probably want to centralize the use of the Guzzle client class in any component of your Laravel project (for example, in an attribute) instead of creating Client instances on multiple Laravel controllers and components (as many articles and answers suggest).
I created a trait that you can try to use, which allows you to send requests from any component of your Laravel project, simply using it and calling makeRequest .
namespace App\Traits; use GuzzleHttp\Client; trait ConsumesExternalServices { public function makeRequest($method, $requestUrl, $queryParams = [], $formParams = [], $headers = [], $hasFile = false) { $client = new Client([ 'base_uri' => $this->baseUri, ]); $bodyType = 'form_params'; if ($hasFile) { $bodyType = 'multipart'; $multipart = []; foreach ($formParams as $name => $contents) { $multipart[] = [ 'name' => $name, 'contents' => $contents ]; } } $response = $client->request($method, $requestUrl, [ 'query' => $queryParams, $bodyType => $hasFile ? $multipart : $formParams, 'headers' => $headers, ]); $response = $response->getBody()->getContents(); return $response; } }
Note that this trait may even handle sending files.
If you want to learn more about this and other features in order to integrate this feature into Laravel, check out this article . In addition, if you are interested in this topic or you need serious help, you can take my course , which will guide you through the whole process.
Hope this helps all of you.
Regards :)
JuanDMeGon Aug 27 '19 at 22:22 2019-08-27 22:22
source share