Shorten long URLs with Perl, cURL, and the API


### This is the long URL we wish to ultimately shorten
$url = "";

sub body_callback {
        my ($chunk,$context)=@_;
        push @{$context}, $chunk;
        return length($chunk);

use Curl::easy;

my $curl= Curl::easy::init();
Curl::easy::setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
Curl::easy::setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
Curl::easy::setopt ($curl, CURLOPT_URL, "$url");
Curl::easy::setopt ($curl, CURLOPT_WRITEFUNCTION, &body_callback);
my @body;
Curl::easy::setopt ($curl, CURLOPT_FILE, @body);
Curl::easy::setopt ($curl, CURLOPT_ERRORBUFFER, "errorBuffer");

if (Curl::easy::perform ($curl) != 0) {
	print "Fail: $errorBuffern";
else {
	### Now we have the URL stored within @body for our use.
	print "Success: @bodyn";


Leave a Reply

Your email address will not be published. Required fields are marked *