Drupal 7 ShURLy Module

By shane
Mon, 2013-03-04 01:07
comments
Daily Dose of Drupal Episode #121

Share with Others

The Drupal 7 ShURLy module provides a simple way to build your own URL shortening service on a Drupal 7 website. The ShURLy module provides an easy way to shorten URLs, manage the URL after it is shortened, and track the clicks of that URL. The Drupal 7 module also provides an easy way to expose the newly created URL shortening service to the rest of the world with an easy to implement web service.

In this episode you will learn:

  • How to use the Drupal ShURLy module to create shortened URLs
  • How to manage shortened URLs created with the Drupal 7 ShURLy module
  • How to use the ShURLy service module to expose the URL shortening web service

Thanks to Drupalize.me for sponsoring this episode of the Daily Dose of Drupal.

Hello everyone and welcome to another Daily Dose of Drupal, today we’re on Episode Number 121 and today we’re going to be going over the Shurly Module and if you ever used a URL Shortening servers like Bitley or any of those other tiny URL Shortening services then you may or you probably already understand what the Shurly Module does.

Basically it allows you to implement your own URL shortening services just by installing a Drupal Module. So it’s relatively easy to set up and get started but before we do; as always I’m Shane Thomas, you can follow me on Twitter at smthomas3, you can also go to codekarate.com and sign up for the newsletter here on the left. Today’s sponsor is drupalize.me.

Drupalize.me is one of the best places to lean about Drupal and J Query, they provide tons of useful videos, some of them are free so go ahead and browse their videos and if you do decide to sign up which I highly recommend, use the coupon code CK20FEB and you’ll get 20% off. So let’s go ahead and get started; I already have the Shurly Module downloaded and installed on the Drupal site.

So if I do a quick search you can see it comes with two modules Shurly and Shurly Service and I’ll show you what the difference is and what each one does here in a second. Make sure you configure the permissions correctly. I’m not going to worry about that in this case but you can set permissions, it’s pretty flexible.

I’m just going to go ahead and click the Configure options so you can see what options you have. Basically it lists all the roles on your site and you can limit how many requests each role can actually place. So since I already have administrators as the only role that can create short URLs, only administrators are shown here but if you allowed anonymous users or authenticated user’s you’d have other options and you can limit how many they can actually submit in a specific period of time and that’s just to make sure you don’t have anyone abusing your URL Shortening Service.

So the first thing I’m going to do is I’m going to do is I’m going to go ahead and click Create URL. So I’ll go ahead and go to drupalize.me and find a video here, I’ll just select the first video and you can see it’s a relatively long URL so I’m going to copy this URL here and I’m going to come back to our test Drupal site and keep in mind My URL isn’t exactly short here test4.codekarate.com but this can be whatever you needed to be so if you if you owned a shorter domain you can of course make use of this and it will make a lot more sense.

So I’m going to drop in the entire URL, I can specify my own URL here if I want or I just click Shrink and it will create one for me. I can copy this over here and I’ll go ahead and paste it in. You can see its only three extra characters on the end of my full domain here and I click Enter and of course it redirects me directly to the site.
So as you can see really easy to get started, really easy to use, all you need to do to get this functionality is turn on this Shurly Module and you can click the Create URL page which is just at /shurly and you can start creating your own URLs.

The other thing to notice here is there’s a My URLs page. Each user has their own URL’s page and this is just a view so it’s just using the Drupal Views Module but it tracks how many clicks have actually been created or actually had been click through. So if I go here and do this again and I come back it knows enough that it doesn’t count duplicate clicks but what it does is it just tracks how many people have click on this links so if you create this shortened link and send it out through Facebook posts or Twitter it will go ahead and track how many people have actually click on that link and used it to redirect to wherever the long URL is that you’re trying to redirect to.

It also list the date when it was last used so let’s go ahead and grab another one here. I’m just going to grab a Code Karate video and I’ll drop this is and this is Code Karate video Episode 120 so I’m going to create my own URL, it’s going to shrink it and I can copy it, paste it in. You can see it might whatever URL I want which is CK120, I hit Enter and it will of course brings me to the Code Karate page. Come back to MY URL and of course you can see both of them are listed, I can get rid of the ones I no longer want to redirect.

There’s also a short URLs page which lists as you can see a couple of other URLs that have been here in the past that are now deactivated but it just list all the URL’s in the site, allows you to filter it. This is again just another View so you can keep in mind how this module makes use of the Views Module to create its administrative interface and the other interfaces that it uses.

Now we’re actually going to take a look at what this other module that comes with what it allows you to do. This is where it gets really cool if you wanted to build your own completely functional URL Shortening Service. So there’s a Shurly Service Module which is turned on and I’m going to go ahead and open up the readme files. This first readme file is just a readme file for the entire or the main Shurly module and you can go ahead and read this over.

It’s similar to what’s on the drupal.org page so there’s not much new there but if I go ahead open up the module there’s a Shurly_service directory and there’s a readme.txt file so if you open that up it tells you how you can use basically a web service to go ahead and create the actual short URLs from either using API call getting Jason back or a text format, xml, you can get a PHP serialized array back.

So there’s a whole bunch of different ways that you can go ahead and automatically create URLs. So can build this into some other website that you’re building to automatically shorten that can be built into a Twitter Tool, something like that. Basically use your imagination to find a lot of ways you can use this so I’m going to go ahead and show you how it works.

I’m going to take this example here and I will come up here and paste it in, I’m going to of course have to change this to test4.codekarate.com, it’s going to be /shurly/apishorten.

The long URL I will go ahead and grab another Daily Dose of Drupal video and I will drip this into the long URL portion. Now if I hit enter you can see it returns a Jason String. Jason string says it was successful which is true, there’s no errors. He was the long URL that you submitted and now here’s the new short URL.

You can see that the slashes are … they have a trailing or leading backslash to escape the slashes but it gives you the URL that you could then use that Jason to use them in whatever program you’re writing. You can of course change the format just by adding this little format indicator at the end of it. So if I want a text format simply add this ampersand format = text and now I just get the actual URL in plain text format, if I go to it it will of course redirect me to the page or the long URL that I was saving.

You can also change this to XML format, PHP format. It mentions how you can use API keys simply by going to this URL here and it will give you the API key that you can then use. So as you can see it’s an extremely flexible way and extremely simple way to build out a fully functional URL Shortening Service similar to Bitley or Tiny URL or any of those other URL shortening services that are out there.

It’s a drop in module, you simply drop it in, turn it on, do a tiny bit of configuration if necessary and you have yourself a working URL shortener. This can be used as a complete service as I’ve shown here or it can be used on top of other modules on your Drupal site if you wanted this to be just a portion of it.

Its main focus is or main purpose is to actually be a complete service so your main sites, the whole goal is just via URL shortener but like I said it can work both ways, go ahead and give it a try and let me know what you think. Thanks again to drupalize.me for sponsoring this episode of the Daily Dose of Drupal.

If you’d like to become a sponsor simply go to the codekarate.com, click on the Daily Dose of Drupal and click on the Code Karate sponsorship link here, gives you a lot of information and you can go ahead and contact me through there. Thank you for watching the Daily Dose of Drupal and I will see you next time.