HTTP Compression Tester
lets you verify that web content is being compressed
using gzip / deflate
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function gzip_tester($url) | |
{ | |
$result = array( | |
'url' => $url, | |
'compressed' => '-', | |
'compressed_type' => '-', | |
'uncompressed_size' => '-', | |
'compressed_size' => '-', | |
'percentage' => '-' | |
); | |
$original = get_header($url); | |
$compress = get_header($url, true); | |
if (count($original) > 1) | |
{ | |
$result['compressed'] = 'no'; | |
$result['uncompressed_size'] = $original['size_download'] . ' bytes'; | |
$header = get_headers_response($compress); | |
if (count($header) > 1) | |
{ | |
$compression = $header["content-encoding"]; | |
$size = $header["content-length"]; | |
if ($compression != "" && $size != "") | |
{ | |
$result['compressed'] = "yes"; | |
$result['compressed_type'] = $compression; | |
$result['compressed_size'] = $size . ' bytes'; | |
$result['percentage'] = number_format(($original['size_download'] - $size) / $original['size_download'] * 100, 1) . ' %'; | |
} | |
} | |
} | |
return $result; | |
} | |
function get_header($url, $com = false) | |
{ | |
$info = array(); | |
$c = curl_init($url); | |
if ($com) | |
{ | |
curl_setopt($c, CURLOPT_ENCODING, "gzip,deflate"); | |
} | |
curl_setopt($c, CURLOPT_HEADER, true); | |
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'); | |
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($c, CURLOPT_REFERER, $url); | |
if (!curl_error($c)) | |
{ | |
if ($com) | |
{ | |
$info = curl_exec($c); | |
} | |
else | |
{ | |
curl_exec($c); | |
$info = curl_getinfo($c); | |
} | |
curl_close($c); | |
} | |
return $info; | |
} | |
function get_headers_response($response) | |
{ | |
$headers = array(); | |
$header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
foreach (explode("\r\n", $header_text) as $i => $line) if ($i === 0) $headers['http_code'] = $line; | |
else | |
{ | |
list($key, $value) = explode(': ', $line); | |
$headers[$key] = $value; | |
} | |
return $headers; | |
} | |
echo "<pre>"; | |
print_r(gzip_tester("http://yourwebsite.com/")); | |
echo "</pre>"; | |
?> |
Output:
Array ( [url] => http://yourwebsite.com/ [compressed] => yes [compressed_type] => gzip [uncompressed_size] => 8517 bytes [compressed_size] => 2738 bytes [percentage] => 67.9 % )
Tidak ada komentar:
Posting Komentar