Google Client API报错:参数必须为RenderAccountIssuesRequestPayload实例
数据源
- 使用适配PHP 7.4的Google Client API库
- 借助Google Shopping官方PHP示例库向Google Merchant账户发起请求
问题描述
在script.php文件中调用以下函数时触发致命错误:
public function get_info($accountId) { $status = $this->session->service->merchantsupport->renderaccountissues( $this->session->merchantId, '{ "contentOption": "CONTENT_OPTION_UNSPECIFIED" }'); return $status; }
错误信息
Fatal error: Uncaught TypeError: 参数2传递给Google\Service\ShoppingContent\Resource\Merchantsupport::renderaccountissues()必须是Google\Service\ShoppingContent\RenderAccountIssuesRequestPayload的实例,传入的是字符串,调用位置为/home/tatam0/tadam/test/php_merchant_api/AccountstatusesSample.php第65行,定义位置为/home/tatam0/tadam/test/php_merchant_api/vendor/google/apiclient-services/src/ShoppingContent/Resource/Merchantsupport.php第54行。
相关文档
已查阅renderaccountissues()函数的官方文档及操作指引,但问题仍未解决。
疑问
“必须是RenderAccountIssuesRequestPayload的实例”是什么意思?该如何修改PHP代码让函数正常运行?尝试将JSON字符串转为对象或数组,但无效。
关于错误含义
Google PHP客户端SDK要求这个接口的第二个参数必须传入SDK预定义的请求实体类实例,而非普通的JSON字符串、数组或通用对象。这个类是SDK自动生成的,专门用于封装该接口的请求参数,确保参数格式、类型完全符合API要求。
代码修改方案
直接使用SDK提供的RenderAccountIssuesRequestPayload类创建实例,并设置参数后传入接口:
public function get_info($accountId) { // 实例化请求实体类 $requestPayload = new Google\Service\ShoppingContent\RenderAccountIssuesRequestPayload(); // 设置contentOption参数 $requestPayload->setContentOption("CONTENT_OPTION_UNSPECIFIED"); $status = $this->session->service->merchantsupport->renderaccountissues( $this->session->merchantId, $requestPayload); return $status; }
为什么数组/普通对象无效?
Google PHP SDK为每个API接口都定义了对应的实体类,这些类继承自统一的基类,SDK内部会依赖类的内置方法完成参数序列化、格式校验等操作。普通数组或对象不具备这些方法,无法被SDK正确处理,因此必须使用指定的实体类实例。
内容的提问来源于stack exchange,提问作者shurc

