ShortURLs

Summary: shorten URLs using bit.ly, including a markup expression and a function for further coding
Version: 2009-07-22
Prerequisites: uses JSON, which requires PHP 5.2.0
Status:
Maintainer: overtones99

Questions answered by this recipe

  • How can I automatically shorten long urls using online resources like [(approve links) edit diff]?
  • Is there a PHP function I can use to create and feed shortened urls to other functions?

Description

shorten URLs using bit.ly

Notes

"URL Shortening" is a way of converting long, perhaps even multiline, URLs into much, much shorter, more readable ones. It's often used on sites like Twitter where there is a character-limit to messages sent. For example, this cookbook page can also be reached by via the shortened URL, [(approve links) edit diff].

There are many URL shortening services available, and so far this page only deals with those generated by [(approve links) edit diff]. Feel free to add code for other URL shortening services as well.

The attached code provides two options for URL shortening:

  • (:BitLyUrl ... :) - a markup expression that converts any single enclosed URL into a bit.ly url
    (:BitLyUrl http://pmwiki.org/wiki/Cookbook/ShortURLs :)
    will output http://bit.ly/VaBzD on the current page
  • bitly_shortened_url($bitLy_login, $bitLy_apiKey, $url) - a function that can be used in other recipes that would enjoy a shortened URL. for example: in a recipe that automatically sends posts to Twitter, or anywhere really where space is tight, one might desire using this function...

To use this code:

Download bit.ly.urlShortening.phpΔ.
in config.php, add the following:

    ## BIT.LY URL SHORTENING
    $bitLy_login = 'yourLogin';
    $bitLy_apiKey = 'yourApiKey';
    include_once("$FarmD/cookbook/bit.ly.urlShortening.php");

Note that you must first register for a login and apikey from the [(approve links) edit diff] site - the registration is free.

Brief Explanation:

This code uses bit.ly's very basic API, which involves sending a call to the bit.ly URL, with arguments attached, like so:

For more information on the API, visit http://code.google.com/p/bitly-api/wiki/ApiDocumentation.
This code was derived from an example on James Cridland's website, [(approve links) edit diff]

Release notes

  • 2009-07-22: Added to PmWiki Cookbook...

See also

Contributors

  • overtones99 (feel free to edit this recipe. i'm not always available to maintain this sort of thing, so feel free to dive in and amend this...)

Comments

This space is for User-contributed commentary and notes.
Please include your name and a date (eg 2007-05-19) along with your comment.
Optional alternative: create a new page with a name like "ThisRecipe-Talk" (e.g. PmCalendar-Talk).

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.