Response metadata

  • The PHP client library logs response metadata, including a request ID, by default.

  • You can programmatically obtain response metadata by setting the optional parameter withResponseMetadata to true when calling client service methods.

  • After calling client service methods, you can retrieve GoogleAdsResponseMetadata from a relevant object, like a service client or a stream, which contains methods to get the full response metadata and the request ID.

  • For SearchStream, call getResponseMetadata() on the stream object to get GoogleAdsResponseMetadata.

  • For Search and other mutate methods, call getResponseMetadata() on the client object to get GoogleAdsResponseMetadata.

The PHP client library logs 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 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\V25\GoogleAdsResponseMetadata)#51 (1) {
  ["metadata":"Google\Ads\GoogleAds\Lib\V25\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:

$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:

// 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.