You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 20:15:57