Convert url to TinyURL

Sharing long urls by email or on social media sites is never a good idea, this is why most people are using urls shorteners such as biy.ly or Tinyurl. This function takes a single parameter, an url, and will return a short url using the Tinyurl service.

function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}

Usage

Nothing complicated, simply pass an url to the function to get its short url:

$my_url = getTinyUrl('http://www.phpsnippets.info/test-existence-of-a-given-url-with-curl');
echo 'Short url: '.$my_url;

If you want to use this snippet within WordPress, you can do so by using the following within the loop:

getTinyUrl(get_permalink($post->ID));

One Comment

  1. Nico
    Posted June 27, 2010 at 3:55 pm | Permalink

    Useful! Thanks

2 Trackbacks

  1. [...] This post was mentioned on Twitter by Jean-Baptiste Jung, PHP Snippets. PHP Snippets said: PHP snippet: Convert url to TinyURL http://bit.ly/bsIA3z #php [...]

  2. By 10 life-saving PHP snippets on July 19, 2010 at 4:07 pm

    [...] { return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); }Source: http://www.phpsnippets.info/convert-url-to-tinyurlCalculate age using date of birthPass a birth date to this function, and it will return the age of [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>