Save bandwidth by getting your reports compressed on the wire

Friday, August 12, 2005


Want to save bandwidth downloading reports? Here's how!

rfc2616 defines a HTTP/1.1 header called Accept-Encoding. This lets HTTP clients ask for a compressed response using a header such as:

Accept-Encoding: gzip
Google servers support gzip encoding, but only for certain HTTP User-Agents. The easiest way to tell Google you really understand gzip encoding is to have the String "gzip" as part of the User-Agent, in addition to using the "Accept-Encoding: gzip" header. If you do that, reports will be transparently compressed and you'll save a lot of download time and bandwidth.

The HTTP User-Agent is different from the useragent SOAP header you use the AdWords API calls: one is used at the transport layer, the other at the application layer. We suggest that you just append the String "gzip" to the default HTTP user agent provided by your toolkit.

This is a PHP 4 sample showing how it's done (it uses libcurl):

  // open connection to the Google server via cURL
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_URL, $reportURL['getReportDownloadUrlReturn']);
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlConnection, CURLOPT_ENCODING, "gzip");
curl_setopt ($curlConnection, CURLOPT_USERAGENT, curl_version() . " gzip");
// buffer for downloading report xml
ob_start();
curl_exec($curlConnection);
// buffer report
$reportXml = ob_get_contents();
ob_end_clean();
// end buffering
if (curl_errno($curlConnection)) {
echo "Sorry, there was a problem while downloading your report. The cURL error message is:".curl_error($curlConnection);
return;

(Thanks to Thomas Steiner for the sample.)

Setting HTTP headers can be done easily with any toolkits you use; keep an eye on the AdWords API Forum where other developers may post samples showing how to set these headers using other languages/libraries.

I hope this tip will help you save bandwidth when getting your API reports.

--Patrick Chanezon, AdWords API evangelist