【发布时间】: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_ListAccountsResponse、Google_Service_MyBusiness_Location 在app/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