Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created December 13, 2020 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/a7d4b2da5b9a96e0f13d7e48a34ec80c to your computer and use it in GitHub Desktop.
Save bitsnaps/a7d4b2da5b9a96e0f13d7e48a34ec80c to your computer and use it in GitHub Desktop.
PHP json string compression different examples and comparison
<?php
$users = file_get_contents('https://jsonplaceholder.typicode.com/users');
$gz = base64_encode(gzencode($users));
if (!$gz){
die('content contains non base64 alpha');
}
echo $gz;
echo '<br>gzencode: ';
echo(strlen($gz));
echo '<br>';
$gzc = base64_encode(gzcompress($users));
if (!$gzc){
die('content contains non base64 alpha');
}
echo $gzc;
echo '<br>gzcompress: ';
echo(strlen($gzc));
echo '<br>';
$gzd = base64_encode(gzdeflate($users));
if (!$gzd){
die('content contains non base64 alpha');
}
echo $gzd;
echo '<br>gzdeflate: ';
echo(strlen($gzd));
echo '<br>';
$json = gzinflate(base64_decode($gzd));
$ob = json_decode($json);
if(is_null($ob)) {
die('json is not valid');
}
die('json is valid');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment