【问题标题】:Google API My Business + PHP + Laravel - class not foundGoogle API My Business + PHP + Laravel - 找不到类
【发布时间】:2018-11-26 09:17:38
【问题描述】:

我使用 OAuth 2.0 流程 + google-api-php-client + Mybusiness PHP 类,但出现“找不到类”错误,见下文:

use App\Vendors\Google_Service_MyBusiness as MyBusiness;
....

$gClient->setAccessToken($access_token);
$oauth2 = new \Google_Service_Oauth2($gClient);
$userInfo = $oauth2->userinfo->get();
print_r($userInfo); //Works!

$mybusinessService = new MyBusiness($gClient);

//return Class 'Google_Service_MyBusiness_Location' not found:
$mybusinessService->accounts->get('accounts/xxxxxxxx');

//return 'Google_Service_MyBusiness_ListAccountsResponse' not found:
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();

完全错误:

Class 'Google_Service_MyBusiness_Account' not found in file /vendor/google/apiclient/src/Google/Http/REST.php on line 128

自动加载中添加的 MyBusiness 类:

"autoload" : {
    "files": [
        "app/Vendors/MyBusiness.php"
    ]
}

所有必需的类(例如Google_Service_MyBusiness_ListAccountsResponseGoogle_Service_MyBusiness_Locationapp/Vendors/MyBusiness.php 中)

我的代码有什么问题?

【问题讨论】:

  • 您是否使用作曲家进行设置?其他自动加载是否有效?我不确定这段代码到底在哪里,它是否在其他东西调用的某个类中?如果它在一个单独的文件中,我希望看到一些引导代码来包含自动加载文件,比如 laravel 索引文件中的this line
  • @mickadoo 我使用作曲家。代码在控制器中。其他自动加载工作。文件app/Vendors/MyBusiness.php 有很多类:Google_Service_MyBusiness 类在控制器中可见并且可以工作,但该文件中的其他类在vendor/google/apiclient/src/Google/Http/REST.php 中不使用。对你来说是完整的答案吗?
  • 您是否尝试过手动要求包含该类的文件?如果可行,那么它应该将其缩小到自动加载问题。如果你修改了一些自动加载的东西,你总是可以尝试composer install,它应该会重新生成自动加载文件
  • @mickadoo 你能给我一个示例代码吗?我对 Laravel 不是很有经验。邮件问题是app/Vendors/MyBusiness.php 中声明的类之一必须在vendor/google/apiclient/src/Google/Http/REST.php 中使用(参见github.com/googleapis/google-api-php-client/blob/master/src/… 第128 行)并且不可见。
  • 我不确定是什么问题,但可能是自动加载。如果您在 app/Vendors/MyBusiness.php 中有 Google_Service_MyBusiness_Account 类,您可以通过手动将此行添加到您的 index.php 或在调用问题代码之前的任何位置来确保事情首先工作:require_once __DIR__ . '/path/to/MyBusiness.php';(替换 /path/到真实路径。如果可行,您可以继续调试为什么自动加载不起作用

标签: php laravel-5 google-api-php-client google-api-client google-my-business-api


【解决方案1】:

在函数 listAccounts() 处更改,第 1262 行

来自:

return $this->call('list', array($params), "Google_Service_MyBusiness_ListAccountsResponse");

到:

return $this->call('list', array($params), "App\Services\Integrations\GoogleApi\Google_Service_MyBusiness_ListAccountsResponse");

【讨论】:

    【解决方案2】:

    供应商目录不在App/Vendor内。

    这样试试

    $service = new \Google_Service_MyBusiness($client);
    

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2020-08-05
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多