我正在使用Composer安装Google API客户端Packagist Google API Client.

在我使用Composer安装后,需要Google/apiclient,它会像正常一样安装. 之后,我try 在下面的示例中使用它

<?php
use Google\Service\ShoppingContent;
use Google\Auth\Credentials\ServiceAccountCredentials;

/**
 *
 */
class GoogleShoppingContentAPI
{
  private $service;

  public function __construct($credentialsPath) {
    // Create the credentials object
    $credentials = new ServiceAccountCredentials(
        ShoppingContent::CONTENT,
        $credentialsPath
    );
    // Create the Shopping Content Service with the credentials
    $this->service = new ShoppingContent(['credentials' => $credentials]);
  }

  public function getProducts($merchantID)
  {
    try {
      // Fetch a list of products
      $products = $this->service->products->listProducts($merchantID);
      // Return the product data as an array
      return $products->getResources();
    } catch (Google\Service\Exception $e) {
      throw new Exception("Error: " . $e->getMessage());
    }
  }
}

但是,在运行代码时,它会打印出以下错误:

Class Fatal error: Uncaught Error: Class "Google\Auth\Credentials\ServiceAccountCredentials" not found in /Users/Shared/www/inc/google.inc.php on line 15

它在我们的自动加载中,在运行上面的代码之前,我们需要自动加载. Google命名空间也不会被另一个包占用.

我已经判断了我们的项目,没有发现任何与Google相关的重复名称空间. 此外,重新安装aswell Composer更新并没有改变任何事情.

推荐答案

好吧,我承认我有点尴尬,但我在错误的文件夹中运行了composer 要求命令. 这就是为什么它被安装了,但它无法自动加载. 真是我的耻辱.?

Php相关问答推荐

尽管包含目标日期,但日期范围比较仍有意外输出

根据特定运输类别增加WooCommerce购物车总重量

Laravel数据表在一个视图中传递两个实例

Laravel 10 -扩展现有的artisan命令?

将变体设置字段添加到WooCommerce中的特定变量产品

Laravel:如何展平多维Eager 加载的集合

只收取WooCommerce中最高的运费

服务器迁移后无法上载文件-可能存在权限问题

如何在汇总表构建器中操作LALAVEL细丝Sum::Make()值

PHP文件上载问题-";Move_Uploaded_Files";未按预期工作

基于自定义域的每个购物车项目的WooCommerce定制数量输入步骤

显示 woocommerce 中所选变体的属性术语描述

无法在 WordPress 上获取 JSON 数据

将一个表中的行连接为不同表MYSQL中的一列

如何在 Woocommerce 中自动删除旧的已完成订单

如果 Select 的县不是store 所在的县,如何隐藏本地自提?

通过存储库检索 Blade 上的单值数据 出错

批量作业(job)提交错误无法处理所有文档,uris 似乎正确?

如何使用相同的变量使函数参数对于数组和字符串都是可选的

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改