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;
?>Save the code to a file named
minify.php
and run the following command:php minify.php
Output:
p{color:red}