Use Your (Yourls) Own URL Shortening Service with Perl

URL Shortening services like Bit.ly have been all the rage with the popularity of Twitter and other micro-blogging services. As some of us use Twitter quite a bit for sharing links to various sites and blog posts, we’re sending lots of traffic to people that isn’t easily identifiable beyond the URL shortening service being used.

Bit.ly does a great job of providing statistics on the URLs that you’re sharing. However, it doesn’t allow the receiver of the traffic to easily know anything about who you are or your brand. I felt if I was providing enough traffic to a publisher, it would make sense to use my own URL shortener so the publisher could easily get more information about me.

I dug around and found quite a few open source shorteners. I eventually decided to use Yourls for a few reasons:

  1. It’s open source
  2. It’s written in PHP and can be deployed easily, even on shared hosting services
  3. The developer of Yourls, Richard Ozh is really quick about fixing bugs and implementing new features.
  4. The feature list may not be as complete as Bit.ly but it comes really close.
  5. Setting up Yourls was a breeze, now came the fun part of doing something with it beyond the bookmarklet. I decided that I would convert all my shared Google Reader items to use Yourls. To do this, I created WWW::Shorten::Yourls which I had introduced in this post.

    Below is a small Perl script which uses the WWW::Shorten::Yourls module to give you a shortened URL for use on Twitter, FaceBook, LinkedIn, email, whatever.


    #! /usr/local/bin/perl
    # $Id: yourls.pl 165 2009-10-23 16:58:32Z pjain $
    # $LastChangedRevision: 165 $
    # $Author: pjain $
    # $Date: 2009-10-23 22:28:32 +0530 (Fri, 23 Oct 2009) $
    # Author: Pankaj Jain
    # Copyright: (c) Teknatus Solutions LLC 2009
    # License: This code is available under the same terms as Perl itself
    ########################################################
    use warnings;
    use strict;
    use Carp;
    use WWW::Shorten::Yourls;

    my $url = “http://www.teknatus.com”;
    my $uid = “my_yourls_id”; #change this
    my $passwd = “my_yourls_password”; #change this
    my $base = “http://yourls.teknatus.com”; #change this to a valid install of yourls

    my $yourls = WWW::Shorten::Yourls->new(USER => $uid,
    PASSWORD => $passwd,
    BASE => $base);
    $yourls->shorten(URL => $url);

    print $yourls->{url}; # print the shortened URL
    print $yourls->expand(URL => $yourls->{url}); # print the original URL by passing the shortened URL as a parameter

    # or do it like this
    print makeashorterlink($url,$uid,$passwd,$base);
    print makealongerlink($yourl,$uid,$passwd,$base);

    1;


Posted

in

by

Tags: