响应元数据

默认情况下,PHP 客户端库会记录响应元数据(包括请求 ID)。或者,您也可以在调用客户端服务方法时以编程方式获取响应元数据,方法是将可选参数 withResponseMetadata 设置为 true

以下示例展示了如何将查询发送到 GoogleAdsService.SearchStream() 并将 withResponseMetadata 设置为 true

[$response, $metadata] = $googleAdsServiceClient->searchStream(
    $customerId,
    $query,
    ['withResponseMetadata' => true]
);

系统将返回一个数组,其中 $responseSearchGoogleAdsStreamResponse 中的查询结果组成。

数组中的第二个成员 $metadata 将响应元数据保存在 metadata 字段中,该字段本身是一个数组:

object(Google\Ads\GoogleAds\Lib\V16\GoogleAdsResponseMetadata)#51 (1) {
  ["metadata":"Google\Ads\GoogleAds\Lib\V16\GoogleAdsResponseMetadata":private]=>
  array(17) {
    ["content-disposition"]=>
    array(1) {
      [0]=>
      string(10) "attachment"
    }
    ["request-id"]=>
    array(1) {
      [0]=>
      string(22) "REQUEST_ID"
    }
    ...
  }
}

与使用 GoogleAdsResponseMetadata::getRequestId() 类似,您可以从 request-id 键检索请求 ID。