Create DiggBar Short URLs with PHP

April 4, 2009 by JDStraughan · 2 Comments
Filed under: php 

One of the best ways to drive traffic to a site is social bookmarking.  With social networking and micro blogging becoming more popular, short URLs have become important for anyone trying to describe something in 140 characters or less.  When you put short URLS and social bookmarking together, you get a powder keg ready to drive massive amounts of traffic to your link.

Now that Digg.com has added the DiggBar with short URLs, you can make adding social bookmarking to your short URLs very simple.

According to Digg the Blog:

The DiggBar allows you to…

  • Digg directly on the destination site: No more awkward toggling between the story page and Digg.
  • Easily share stories: You can now create a shortened Digg URL to share on Twitter, Facebook or via email. You can also type digg.com/ before the URL of any page you’re on to create a short URL.
  • Access additional analytics: See how many times a story has been viewed.
  • View comments while on the story page: Clicking the ‘Comments’ button expands the DiggBar to show the top comment, latest comment, and most controversial without leaving the page.
  • Discover related stories: Clicking the ‘Related’ button expands the DiggBar to highlight similar stories.
  • See more stories from the same source: Clicking the ‘Source’ button expands the DiggBar to show you more Digg stories from that source site.
  • Discover random stories: Click the ‘Random’ button and you’ll be brought to an entirely new, unexpected story.

Using the Digg Short URL portion of the Digg API and PHP’s XML Parser, we will be able to kill 2 birds with one stone: allow our pages to be easily Diggable, and provide shortened URLs.

This function utilizes curl to contact Digg, which sends a reply in XML format.  Using PHP’s XML Parser, we grab the portion of the XML we need for our short URL and return it.

1
2
3
4
5
6
7
8
function digg_url($longurl)
{
	$simple = shell_exec('curl http://services.digg.com/url/short/create?url='. urlencode($longurl));
	$p = xml_parser_create();
	xml_parse_into_struct($p, $simple, $vals, $index);
	xml_parser_free($p);
	return $vals[1]['attributes']['SHORT_URL'];
}

I use this function when my site, TUTlist, has a new submission approved.  I get the long url, and convert it to a small one and then send it out on my Twitter account.

Apart from Twitter, short URLs can be used in all sorts of applications, and with the DiggBar addition, the ability for your pages to be ‘Dugg’ is a great resource of traffic.  This is the primary reason I moved to the Digg service from tinyURL.

This implementation of this function is very straight forward.  For the sake of simplicity, we will hard code the long URL.  In real world scenarios, this URL would most likely be user input or pulled from a database.

1
2
3
4
5
$longurl = 'http://tutlist.com/tutorial/details/create-diggbar-short-urls-with-php';
 
$shorturl = digg_url($longurl);
 
echo $shorturl;

Will produce:

http://digg.com/u1DIP

This can, in turn, be used in tweets, posting, or any application where a short URL and a Digg button will be of benefit.

Have fun making all your long URLs short, and feel free to leave your comments, complaints, or suggestions below.

  • Share/Bookmark

Best online pastebins for code collaborations

January 14, 2009 by JDStraughan · Leave a Comment
Filed under: Tools 

Online pastebins are a great resource for sharing small bits of code with other developers.  Most often, these are used to debug or refactor isolated code,  and is a very popular tool in IRC chat rooms.  The most common site is pastebin.com, and although it is probably the most used, it has not had a facelift in ages.

Modern pastebins allow for code execuction, custom urls, commenting, and minor collaboration tools.  Oh, and they beat trying to send 200 lines of javascript in your instant messenger client.

Both of these sites should be part of any web developer’s toolkit.

CODEPAD.ORG

For most code, a relatively vanilla site codepad.org offers syntax highlighting, and online compiler/interpreter, and the ability to have a unique URL for your organization.

According to their ‘about’ page:

“codepad.org is an online compiler/interpreter, and a simple collaboration tool. It’s a pastebin that executes code for you. You paste your code, and codepad runs it and gives you a short URL you can use to share it. Paste the URL into chat or email to get help or to show someone how to do something. Or just try things out when you don’t have an interpreter handy. It works well on many phones.”

Visit codepad.org

JSBIN.COM

For those of us who use one of the many flavors of JavaScript libraries (jQuery, MooTools, etc), there is jsbin.com.  Like codepad, it will interpret your code and show the result of your script.  The ability to choose your library of choice, drop in isolated snippets, and share with your peers far surpasses any other online tool I have found for sharing JavaScript code.

According to their website:

JS Bin is a webapp specifically designed to help JavaScript and CSS folk test snippets of code, within some context, and debug the code collaboratively.

JS Bin allows you to edit and test JavaScript and HTML (reloading the URL also maintains the state of your code - new tabs doesn’t). Once you’re happy you can save, and send the URL to a peer for review or help. They can then make further changes saving anew if required.

The original idea spawned from a conversation with another developer in trying to help him debug an Ajax issue. The original aim was to build it using Google’s app engine, but in the end, it was John Resig’s Learning app that inspired me to build the whole solution in JavaScript with liberal dashes of jQuery and a tiny bit of LAMP for the saving process.

Visit jsbin.com

  • Share/Bookmark

A new year, a new blog.

January 6, 2009 by JDStraughan · Leave a Comment
Filed under: Rants 

I have never been good at keeping a blog going more than a week or two.  I am also not big on New Year resolutions, however, I need to start having a place to post some code and write some fun stuff, and I would also enjoy some community feedback to make me (and hopefully you) a better developer.  Provided that I cannot stand ‘microblogging’ and am only mildly interested in social bookmarking, a blog seems like a logical place to turn.

Hopefully this is a place I use often, post some fun snippets, and create conversations with web developers of all skill levels.

Happy (belated) New Year!

  • Share/Bookmark