Skip to content

Instantly share code, notes, and snippets.

@dovidezra
Created October 8, 2022 05:01
Show Gist options
  • Save dovidezra/2d87599cd7deab6fbbd2ad0282b4c351 to your computer and use it in GitHub Desktop.
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
<?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;
?>
@dovidezra
Copy link
Author

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 accepted
406 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment