Shorten long URLs with Perl, cURL, and the short.to API

#!/usr/bin/perl

### This is the long URL we wish to ultimately shorten
$url = "http://ww2db.com/battle_spec.php?battle_id=10";



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, "http://short.to/s.txt?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";
};

Curl::easy::cleanup($curl);

Leave a Reply

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