[codesyntax lang="php"]

<?php

$doCompress = FALSE;

$output = file_get_contents('http://www.sina.com.cn');

error_log('Content length before gzip: ' . strlen($output) . "\r\n", 3, 'd:/data.log');

if (extension_loaded("zlib")
    && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
    $doCompress = TRUE;
    ob_start();
    ob_start('ob_gzhandler');
} else {
    error_log('Cannot finish gzip compress!' . "\r\n", 3, 'd:/data.log');
    header("Content-length: " . strlen($output));
}

print($output);

if ($doCompress) {
    ob_end_flush();
    header("Content-length: " . ob_get_length());
    error_log('Content length after gzip: ' . ob_get_length() . "\r\n", 3, 'd:/data.log');
    ob_end_flush();
}

[/codesyntax]