The PHP client library [logs](https://developers.google.com/google-ads/api/docs/client-libs/php/logging.md.txt) the response metadata, including a
request ID, by default. Alternatively, you can obtain the response metadata
programmatically when calling client service methods by setting the optional
parameter `withResponseMetadata` to `true`.

After you call client service methods, you can obtain a
[`GoogleAdsResponseMetadata`](https://github.com/googleads/google-ads-php/blob/HEAD/src/Google/Ads/GoogleAds/Lib/V26/GoogleAdsResponseMetadata.php) instance from the relevant
object (such as a service client or a stream) depending on the method you call.
This object provides `getMetadata()` and `getRequestId()`, which return the full
response metadata array and the request ID of the API call, respectively. The
`getMetadata()` method returns an array structured like the following output:

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

The `getRequestId()` method simplifies extracting the request ID from the
metadata array without manually parsing the array keys.

The following sections explain how to retrieve `GoogleAdsResponseMetadata` for
each method type.

## SearchStream

To obtain a `GoogleAdsResponseMetadata` object for a streaming call, call
`getResponseMetadata()` on the **stream** object after iterating over the
stream:

```php
$stream = $googleAdsServiceClient->searchStream(
    SearchGoogleAdsStreamRequest::build($customerId, $query),
    ['withResponseMetadata' => true]
);

foreach ($stream->iterateAllElements() as $googleAdsRow) {
    // Process each row...
}

// Outputs the request ID.
echo $stream->getResponseMetadata()?->getRequestId() . PHP_EOL;
```

`$stream->getResponseMetadata()` returns a `GoogleAdsResponseMetadata` object.

## Search and mutate methods

To obtain a `GoogleAdsResponseMetadata` object for unary calls, call
`getResponseMetadata()` on the **service client** object:

```php
// Retrieves objects.
$response = $googleAdsServiceClient->search(
    SearchGoogleAdsRequest::build($customerId, $query),
    ['withResponseMetadata' => true]
);

// Outputs the request ID.
echo $googleAdsServiceClient->getResponseMetadata()?->getRequestId() . PHP_EOL;

// Mutates campaigns.
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build($customerId, $campaignOperations),
    ['withResponseMetadata' => true]
);

// Outputs the request ID.
echo $campaignServiceClient->getResponseMetadata()?->getRequestId() . PHP_EOL;
```

Both `$campaignServiceClient->getResponseMetadata()` and
`$googleAdsServiceClient->getResponseMetadata()` return a
`GoogleAdsResponseMetadata` object.

> [!CAUTION]
> **Caution:** On service client instances, `getResponseMetadata()` returns the metadata for the most recent request executed with `['withResponseMetadata' =>
> true]` on that client instance. Executing a subsequent request on the same service client instance overwrites the stored metadata.