我尝试在Magento 2.2.9中使用guzzlehttp/guzzle,但它不起作用

xesrikrc  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(150)

它给了我一个错误

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

我已经运行了所有Magento所需的命令,如安装程序:upgrade,di:编译和部署,但它仍然向我抛出这个错误。
我已经检查了GuzzleHttp在供应商文件夹中,它已经安装在Magento 2.2.9
我已经尝试了 composer 要求guzzlehttp/guzzle:^6.0重新安装库,但没有运气。

b09cbbtk

b09cbbtk1#

导入此库:

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

on __构造用途:

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

然后呼叫:

$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);
qlckcl4x

qlckcl4x2#

尝试这种方式来创建GuzzleClient的一个示例,我目前正在使用一个类似的magento 2.4.4和工程罚款,你不必inyect,on __construct()

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

相关问题