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

如何正确使用Google Cloud Healthcare API的HttpBody类setData()方法

问题根因

Google\HttpBody类的setData()方法需要接收序列化完成的JSON字符串,你当前传入的是PHP关联数组,请求发送时数组没有被正确转换成符合要求的FHIR JSON结构,导致服务端无法识别到resourceType字段。

解决方案

  • 给setData()传参前,先把构造好的FHIR资源数组用json_encode()转换为JSON字符串
  • 确认你调用自定义create方法时传入的第一个参数$resource的值为Patient,和payload内的resourceType取值保持一致

修改后的代码如下:

public function create(string $resource, $body)
{
        $client   = $this->googleClient();
        $service  = new CloudHealthcare($client);

        $parent   = "projects/my_project_id/locations/my_location/datasets/my_dataset/fhirStores/repository";

        $httpBody = new HttpBody();
        $httpBody->setContentType('application/fhir+json;charset=utf-8');
        // 新增json_encode处理数组,添加JSON参数避免编码异常
        $httpBody->setData(json_encode([
            "resourceType" => "Patient",
            "id" => "23434",
            "meta" => [
                "versionId" => "12",
                "lastUpdated" => "2014-08-18T15:43:30Z"
            ],
            "text" => [
                "status" => "generated",
                "div" => "<!-- Snipped for Brevity -->"
            ],
            "extension" => [
                [
                    "url" => "http://example.org/consent#trials",
                    "valueCode" => "renal"
                ]
            ],
            "identifier" => [
                [
                    "use" => "usual",
                    "label" => "MRN",
                    "system" => "http://www.goodhealth.org/identifiers/mrn",
                    "value" => "123456"
                ]
            ],
            "name" => [
                [
                    "family" => [
                        "Levin"
                    ],
                    "given" => [
                        "Henry"
                    ],
                    "suffix" => [
                        "The 7th"
                    ]
                ]
            ],
            "gender" => [
                "text" => "Male"
            ],
            "birthDate" => "1932-09-24",
            "active" => true
        ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));

        $data     = $service->projects_locations_datasets_fhirStores_fhir->create($parent, $resource, $httpBody);

        return $data;
}

可选排查步骤

如果修改后依然报错,可以临时打印json_encode的输出结果,确认生成的JSON字符串顶层包含resourceType字段,格式没有异常。


内容的提问来源于stack exchange,提问作者Clesio SAMA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 07:51:00