Created
December 13, 2020 11:20
-
-
Save bitsnaps/a7d4b2da5b9a96e0f13d7e48a34ec80c to your computer and use it in GitHub Desktop.
PHP json string compression different examples and comparison
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 | |
$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