Google API PHP Client调用UsersSettingsSendAs::list()方法报错
解决Google APIs Client Library for PHP调用sendAs列表方法报错问题
问题场景
我们使用最新版本(2.13.2)的Google APIs Client Library for PHP,尝试获取用户的sendAs邮箱列表时触发500错误,执行代码如下:
$gmail = new Google_Service_Gmail($client); $response = $gmail->users_settings_sendAs->list('my@email.tld');
错误日志
Apache日志显示具体错误:
[Mon Apr 24 12:50:37.233655 2023] [proxy_fcgi:error] [pid 4185936:tid 139798719207168] [client 90.161.67.44:35704] AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to undefined method Google\Service\Gmail\Resource\UsersSettingsSendAs::list() in /path/to/index.php:19\nStack trace:\n#0 {main}\n thrown in /path/to/index.php on line 19'
对比验证
调用其他方法(如get)可正常运行:
$gmail = new Google_Service_Gmail($client); $response = $gmail->users_settings_sendAs->get('my@email.tld','my@email.tld')->getSignature();
查看api/vendor/google/apiclient-services/src/Gmail.php文件,确认get和list方法的定义都存在:
$this->users_settings_sendAs = new Gmail\Resource\UsersSettingsSendAs( $this, $this->serviceName, 'sendAs', [ 'methods' => [ ...,'get' => [ 'path' => 'gmail/v1/users/{userId}/settings/sendAs/{sendAsEmail}', 'httpMethod' => 'GET', 'parameters' => [ 'userId' => [ 'location' => 'path', 'type' => 'string', 'required' => true, ], 'sendAsEmail' => [ 'location' => 'path', 'type' => 'string', 'required' => true, ], ], ],'list' => [ 'path' => 'gmail/v1/users/{userId}/settings/sendAs', 'httpMethod' => 'GET', 'parameters' => [ 'userId' => [ 'location' => 'path', 'type' => 'string', 'required' => true, ], ], ],...
解决方案
问题出在list是PHP的关键字,不能直接作为方法名调用。Google的PHP客户端库会自动对这类方法名做转义处理,有两种调用方式可选:
- 使用规范命名的方法:
$gmail = new Google_Service_Gmail($client); $response = $gmail->users_settings_sendAs->listUsersSettingsSendAs('my@email.tld');
- 使用下划线转义的方法名:
$gmail = new Google_Service_Gmail($client); $response = $gmail->users_settings_sendAs->_list('my@email.tld');
两种方式都能正确触发API的列表查询,避免关键字冲突导致的方法未定义错误。
内容的提问来源于stack exchange,提问作者Marc Serra
相关产品推荐
相关产品推荐

