响应元数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
PHP 客户端库默认会记录响应元数据,包括请求 ID。或者,您也可以在调用客户端服务方法时,通过将可选参数 withResponseMetadata
设置为 true
,以编程方式获取响应元数据。
调用客户端服务方法后,您可以根据所调用的方法,从相关对象(例如服务客户端或流)中获取 GoogleAdsResponseMetadata
。此对象包含 getMetadata()
和 getRequestId()
,分别用于返回响应元数据和 API 调用的请求 ID。getMetadata()
方法会返回如下所示的数组:
object(Google\Ads\GoogleAds\Lib\V21\GoogleAdsResponseMetadata)#51 (1) {
["metadata":"Google\Ads\GoogleAds\Lib\V21\GoogleAdsResponseMetadata":private]=>
array(17) {
["content-disposition"]=>
array(1) {
[0]=>
string(10) "attachment"
}
["request-id"]=>
array(1) {
[0]=>
string(22) "REQUEST_ID"
}
...
}
}
getRequestId()
方法简化了从元数据数组中提取请求 ID 的过程,省去了手动解析的麻烦。
以下部分介绍了如何针对每种方法检索 GoogleAdsResponseMetadata
。
SearchStream
如需获取 GoogleAdsResponseMetadata
对象,请对 stream 对象调用 getResponseMetadata()
:
$stream = $googleAdsServiceClient->searchStream(
SearchGoogleAdsStreamRequest::build($customerId, $query),
['withResponseMetadata' => true]
);
// Prints the request ID.
print $stream->getResponseMetadata()->getRequestId() . PHP_EOL;
$stream->getResponseMetadata()
是 GoogleAdsResponseMetadata
的对象。
搜索和其他变异方法
如需获取 GoogleAdsResponseMetadata
对象,请对 client 对象调用 getResponseMetadata()
:
// Retrieves objects.
$response = $googleAdsServiceClient->search(
SearchGoogleAdsRequest::build($customerId, $query),
['withResponseMetadata' => true]
);
// Prints the request ID.
print $googleAdsServiceClient->getResponseMetadata()->getRequestId() . PHP_EOL;
// Mutates campaigns.
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build($customerId, $campaignOperations),
['withResponseMetadata' => true]
);
// Prints the request ID.
print $campaignServiceClient->getResponseMetadata()->getRequestId() . PHP_EOL;
$campaignServiceClient->getResponseMetadata()
和 $googleAdsServiceClient->getResponseMetadata()
是 GoogleAdsResponseMetadata
的对象。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eThe PHP client library automatically logs response metadata, including a request ID.\u003c/p\u003e\n"],["\u003cp\u003eYou can programmatically access response metadata, including the request ID, using the \u003ccode\u003ewithResponseMetadata\u003c/code\u003e parameter and the \u003ccode\u003eGoogleAdsResponseMetadata\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eGoogleAdsResponseMetadata\u003c/code\u003e object provides methods like \u003ccode\u003egetMetadata()\u003c/code\u003e to retrieve detailed metadata and \u003ccode\u003egetRequestId()\u003c/code\u003e for easy access to the request ID.\u003c/p\u003e\n"],["\u003cp\u003eDepending on the method used (SearchStream, Search, or other mutate methods), you can retrieve the \u003ccode\u003eGoogleAdsResponseMetadata\u003c/code\u003e object from either the stream or the client object.\u003c/p\u003e\n"]]],[],null,["# Response Metadata\n\nThe PHP client library [logs](/google-ads/api/docs/client-libs/php/logging) the response\nmetadata, including a request ID, by default. Alternatively, you can obtain the\nresponse metadata programmatically when calling client service methods by\nsetting the optional parameter `withResponseMetadata` to `true`.\n\nAfter you call client service methods, you can then obtain\n[`GoogleAdsResponseMetadata`](//github.com/googleads/google-ads-php/blob/main/src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsResponseMetadata.php),\nfrom a relevant object, like a service client or a stream, according to the\nmethod you call. This object contains `getMetadata()` and `getRequestId()`,\nwhich return response metadata and the request ID of the API call, respectively.\nThe `getMetadata()` method returns an array that looks like this: \n\n object(Google\\Ads\\GoogleAds\\Lib\\V21\\GoogleAdsResponseMetadata)#51 (1) {\n [\"metadata\":\"Google\\Ads\\GoogleAds\\Lib\\V21\\GoogleAdsResponseMetadata\":private]=\u003e\n array(17) {\n [\"content-disposition\"]=\u003e\n array(1) {\n [0]=\u003e\n string(10) \"attachment\"\n }\n \\[\"request-id\"\\]=\\\u003e\n array(1) {\n \\[0\\]=\\\u003e\n string(22) \"REQUEST_ID\"\n }\n ...\n }\n }\n\nThe `getRequestId()` method simplifies the process of extracting the request ID\nfrom the metadata array, saving you the effort of manually parsing it.\n\nThe following sections explain how to retrieve `GoogleAdsResponseMetadata` for\neach method.\n\nSearchStream\n------------\n\nTo obtain an object of `GoogleAdsResponseMetadata`, call `getResponseMetadata()`\non the **stream** object: \n\n $stream = $googleAdsServiceClient-\u003esearchStream(\n SearchGoogleAdsStreamRequest::build($customerId, $query),\n ['withResponseMetadata' =\u003e true]\n );\n\n // Prints the request ID.\n print $stream-\u003egetResponseMetadata()-\u003egetRequestId() . PHP_EOL;\n\nThe `$stream-\u003egetResponseMetadata()` is an object of\n`GoogleAdsResponseMetadata`.\n\nSearch and other mutate methods\n-------------------------------\n\nTo obtain an object of `GoogleAdsResponseMetadata`, call `getResponseMetadata()`\non the **client** object: \n\n // Retrieves objects.\n $response = $googleAdsServiceClient-\u003esearch(\n SearchGoogleAdsRequest::build($customerId, $query),\n ['withResponseMetadata' =\u003e true]\n );\n\n // Prints the request ID.\n print $googleAdsServiceClient-\u003egetResponseMetadata()-\u003egetRequestId() . PHP_EOL;\n\n // Mutates campaigns.\n $response = $campaignServiceClient-\u003emutateCampaigns(\n MutateCampaignsRequest::build($customerId, $campaignOperations),\n ['withResponseMetadata' =\u003e true]\n );\n\n // Prints the request ID.\n print $campaignServiceClient-\u003egetResponseMetadata()-\u003egetRequestId() . PHP_EOL;\n\nThe `$campaignServiceClient-\u003egetResponseMetadata()` and\n`$googleAdsServiceClient-\u003egetResponseMetadata()` are an object of\n`GoogleAdsResponseMetadata`."]]