Theo mặc định, thư viện ứng dụng PHP ghi nhật ký siêu dữ liệu phản hồi, bao gồm cả mã yêu cầu. Ngoài ra, bạn có thể lấy siêu dữ liệu phản hồi theo phương thức lập trình khi gọi các phương thức dịch vụ ứng dụng bằng cách đặt tham số withResponseMetadata
(không bắt buộc) thành true
.
Sau khi gọi các phương thức dịch vụ khách hàng, bạn có thể lấy GoogleAdsResponseMetadata
từ một đối tượng có liên quan, chẳng hạn như ứng dụng dịch vụ hoặc luồng, theo phương thức bạn gọi. Đối tượng này chứa getMetadata()
và getRequestId()
, lần lượt trả về siêu dữ liệu phản hồi và mã yêu cầu của lệnh gọi API.
Phương thức getMetadata()
trả về một mảng có dạng như sau:
object(Google\Ads\GoogleAds\Lib\V19\GoogleAdsResponseMetadata)#51 (1) {
["metadata":"Google\Ads\GoogleAds\Lib\V19\GoogleAdsResponseMetadata":private]=>
array(17) {
["content-disposition"]=>
array(1) {
[0]=>
string(10) "attachment"
}
["request-id"]=>
array(1) {
[0]=>
string(22) "REQUEST_ID"
}
...
}
}
Phương thức getRequestId()
đơn giản hoá quá trình trích xuất mã yêu cầu từ mảng siêu dữ liệu, giúp bạn không phải phân tích cú pháp mã yêu cầu theo cách thủ công.
Các phần sau giải thích cách truy xuất GoogleAdsResponseMetadata
cho từng phương thức.
SearchStream
Để lấy đối tượng GoogleAdsResponseMetadata
, hãy gọi getResponseMetadata()
trên đối tượng luồng:
$stream = $googleAdsServiceClient->searchStream(
SearchGoogleAdsStreamRequest::build($customerId, $query),
['withResponseMetadata' => true]
);
// Prints the request ID.
print $stream->getResponseMetadata()->getRequestId() . PHP_EOL;
$stream->getResponseMetadata()
là một đối tượng của GoogleAdsResponseMetadata
.
Tìm kiếm và các phương thức thay đổi khác
Để lấy đối tượng của GoogleAdsResponseMetadata
, hãy gọi getResponseMetadata()
trên đối tượng client (ứng dụng khách):
// 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()
và $googleAdsServiceClient->getResponseMetadata()
là một đối tượng của GoogleAdsResponseMetadata
.