Home > Software design >  I'm trying to use guzzlehttp/guzzle in Magento 2.2.9 but it's not working
I'm trying to use guzzlehttp/guzzle in Magento 2.2.9 but it's not working

Time:07-20

It's throwing me an error

PHP Error:  Cannot instantiate interface GuzzleHttp\\ClientInterface in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111, referer: http://127.0.0.1/local_m2/admin/admin/system_config/edit/section/active_campaign/key/6a9ce672c9414c57acd889eb50fb82020e13e651b74cf3a81b9cd8b30da45306/ here

I have already run all Magento required commands Like Setup: upgrade, di:compile and deploy but still it's throwing me this error.

I have already checked GuzzleHttp in the vendor folder, it's already installed in Magento 2.2.9

I have tried the composer require guzzlehttp/guzzle:^6.0 to reinstall the library but having no luck.

CodePudding user response:

import this library:

use GuzzleHttp\Client;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\RequestException;

on __construct use:

$this->client = new Client([
            "base_uri" => url_base,
            "timeout" => 2,
        ]);

and then call:

$response = $this->client->get(
                    url_base . "/" . url_api_point,
                    ['headers' =>
                        [
                            'Authorization' => "Bearer {$this->token}" /* if have */
                        ]
                    ]
                );
                return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

CodePudding user response:

try this way to create an instance of GuzzleClient, im currently using a similar in a magento 2.4.4 and works fine, you don´t have to inyect that on __construct()

/**
 * @return \GuzzleHttp\Client
 */
public function getClient()
{
    $client = new \GuzzleHttp\Client(["base_uri" => "your url", "verify" => false]);
    return $client;
}

  • Related