Skip to main content

Minify CSS web API examples (curl, node.js, perl and php).

# Minify your CSS

## Curl

	# stdin to stdout
	$ curl -X POST -s --data-urlencode 'input=p { color : red; }' http://cssminifier.com/raw

	# from/to file
	$ curl -X POST -s --data-urlencode 'input@style.css' http://cssminifier.com/raw > style.min.css

## Node.js

	var querystring = require('querystring');
	var http  = require('http');

	var query = querystring.stringify({
		input : css
	});

	var req = http.request(
		{
			method   : 'POST',
			hostname : 'cssminifier.com',
			path     : '/raw',
		},
		function(resp) {
			if (resp.statusCode !== 200) {
				console.log('StatusCode=' + resp.statusCode);
				return;
			}
			resp.pipe(process.stdout);
		}
	);

	req.on('error', function(err) {
		throw err;
	});

	req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.setHeader('Content-Length', query.length);
	req.end(query, 'utf8');

## PHP

	<?php
		$url = 'http://cssminifier.com/raw';
		$css = file_get_contents('style.css');
		$data = array(
			'input' => $css,
		);

		$ch = curl_init($url);

		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$minified = curl_exec($ch);

		curl_close($ch);

		echo $minified;
	?>

## Perl

	use strict;
	use warnings;

	use File::Slurp;
	use HTTP::Request::Common qw(POST);
	use LWP::UserAgent;

	sub minify{
		my ($css) = @_;

		my $css_min_url = "http://cssminifier.com/raw";

		my $ua = LWP::UserAgent->new;
		my $request = POST($css_min_url, [ 'input' => $css ]);
		my $css_min = $ua->request($request)->decoded_content();

		return $css_min;
	}

	print minify($css, "\n");

## LWP Request

	$ lwp-request -m POST http://cssminifier.com/raw
	Please enter content (application/x-www-form-urlencoded) to be POSTed:
	input=p%20{%20color:%20white;%20}
	^D

---

> [Minify your CSS - Examples](http://cssminifier.com/examples)