Created
October 8, 2022 05:01
-
-
Save dovidezra/2d87599cd7deab6fbbd2ad0282b4c351 to your computer and use it in GitHub Desktop.
CSS Minifier PHP Example: Check the example on how to use PHP to minify a CSS hardcoded string and output to stdout
This file contains hidden or 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 | |
$url = 'https://www.toptal.com/developers/cssminifier/api/raw'; | |
// init the request, set various options, and send it | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POST => true, | |
CURLOPT_HTTPHEADER => ["Content-Type: application/x-www-form-urlencoded"], | |
CURLOPT_POSTFIELDS => http_build_query([ "input" => "p { color : red; }" ]) | |
]); | |
$minified = curl_exec($ch); | |
// finally, close the request | |
curl_close($ch); | |
// output the $minified CSS | |
echo $minified; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error Status Codes
If something doesn’t go as expected, these are the status code being used by the API:
400 Missing input
405 HTTP Method not allowed - only
POST
is accepted406 Content Type is not acceptable - only
application/x-www-form-urlencoded
is accepted.413 Too large payload - max-size is
5MB
422 Malformed input - for invalid css.
429 Too many requests - currently there is a limit of
30
requests per minute.