Questions answered by this recipe
- How can I automatically shorten long urls using online resources like
- Is there a PHP function I can use to create and feed shortened urls to other functions?
shorten URLs using bit.ly
"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,
There are many URL shortening services available, and so far this page only deals with those generated by
http://bit.ly. 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/VaBzDon 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:
## 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
http://bit.ly site - the registration is free.
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, http://james.cridland.net/code/bitly.html
- 2009-07-22: Added to PmWiki Cookbook...
- 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...)
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).
See discussion at ShortURLs-Talk