Hello Everyone,
In this blog, we will learn about how to Use Guzzle Client for API Operations in Magento 2.
What is Guzzle:
Guzzle is a php http client that makes integration with some web services. Guzzle is one of the most popular tools for making HTTP requests.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Use GuzzleHTTP Client in Magento 2.
STEPS FOR USE GUZZLEHTTP CLIENT IN MAGENTO 2
To Use GuzzleHTTP we require below classes
use GuzzleHttp\Client;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ResponseFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Webapi\Rest\Request;
Step 1: Install GuzzleHttp
In the latest Versions of Magento GuzzleHttp is available by default. But if it is not installed then you will need to install GuzzleHttp Client via composer.
composer require guzzlehttp/guzzle
Step 2: Create Controller file GetData.php
app/code/Vendor/Extension/Controller/Services/GetData.php
<?php
namespace Vendor\Extension\Controller\Services;
use GuzzleHttp\Client;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ResponseFactory;
use Magento\Framework\Webapi\Rest\Request;
/**
* Class GetData Api
*/
class GetData
{
/**
* API request URL
*/
const API_REQUEST_URI = ‘https://api.magecurious.com/’;
/**
* API request endpoint
*/
const API_REQUEST_ENDPOINT = ‘demo/’;
/**
* @var ResponseFactory
*/
private $responseFactory;
/**
* @var ClientFactory
*/
private $clientFactory;
/**
* Constructor
*
* @param ClientFactory $clientFactory
* @param ResponseFactory $responseFactory
*/
public function __construct(
ClientFactory $clientFactory,
ResponseFactory $responseFactory
) {
$this->clientFactory = $clientFactory;
$this->responseFactory = $responseFactory;
}
public function execute()
{
$demoName = ‘magento/magento2’;
$response = $this->doRequest(static::API_REQUEST_ENDPOINT . $demoName);
$status = $response->getStatusCode(); // 200 status code
$responseBody = $response->getBody();
$responseContent = $responseBody->getContents();
return json_decode($responseContent);
}
/**
* Do API request with provided params
*
* @param string $uriEndpoint
* @param array $params
* @param string $requestMethod
*
* @return Response
*/
private function doRequest(
string $uriEndpoint,
array $params = [],
string $requestMethod = Request::HTTP_METHOD_GET
): Response {
/** @var Client $client */
$client = $this->clientFactory->create([‘config’ => [
‘base_uri’ => self::API_REQUEST_URI
]]);
try {
$response = $client->request(
$requestMethod,
$uriEndpoint,
$params
);
} catch (GuzzleException $exception) {
/** @var Response $response */
$response = $this->responseFactory->create([
‘status’ => $exception->getCode(),
‘reason’ => $exception->getMessage()
]);
}
return $response;
}
}
This is a basic example. You have to change/modify this code according to your requirements. You can refer to the official documentation of GuzzleHTTP.
Step 3: Finally run the below commands
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Use GuzzleHTTP Client in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.