Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

Creating a TinyURL with TinyURL’s API

The popular URL shortening service TinyURL provides a quick API that creates TinyURL’s on the fly. Here’s how you can access that API.

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

Make a call to our function above, and it outputs a new tinyURL.

<?php
echo(createTinyUrl('http://www.richardcastera.com/2009/05/09/creating-a-tinyurl-with-tinyurl-api/'));
?>
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

15 Responses to “Creating a TinyURL with TinyURL’s API”

  1. Comment #1 - Permalink
    Native B
    05/27/2009

    Great example – could you please create an example for the api at http://101.gs/ looks similar to tinyurl. A simple web service connection example would be nice ;)

  2. Comment #2 - Permalink
    Richard
    05/28/2009

    Hey Native B,

    Thanks for your comment. There’s a pretty good example of using there API here: http://101.gs/apiexample.php

  3. Comment #3 - Permalink
    dan
    08/10/2009

    Thank you for the example.

    What if:

    I need to make shorter multiple links. All that i need is a shorter url for each.

    Do you have any clue ?

    thank you.

  4. Comment #4 - Permalink
    Richard
    08/11/2009

    @Dan,

    You can create a for loop to generate the shortened URL’s while calling “createTinyUrl()” for all of your links.

  5. Comment #5 - Permalink
    Chris
    08/12/2009

    First off, thanks for this script; you saved me a bunch of time!

    Second, what I would like to do is use this in conjunction with a script I have that pulls the current URL of the page.

    So, in place of “http://www….” in your example, I need to supply your function with the resulting URL of the other function.

    I’ve tested both functions separately, and the both produce the desired output. I just can’t get them to work together.

    Any idea how I might accomplish such a feat?

    Thanks again

  6. Comment #6 - Permalink
    Richard
    08/14/2009

    @Chris,

    Hey so here is how you can accomplish this. Mind you this is a quick example and you should account for failures and invalid return values.

    // Your function to get the current page URL.
    function yourGetPageCurrentURL() {
    return $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }

    // My function to generate a Tiny URL.
    function createTinyUrl($strURL) {
    $tinyurl = file_get_contents(“http://tinyurl.com/api-create.php?url=”.$strURL);
    return $tinyurl;
    }

    $currentURL = yourGetPageCurrentURL();
    echo(createTinyUrl($currentURL));

    Hope this helps ;)

  7. Comment #7 - Permalink
    Tony
    11/09/2009

    We are playing around with using tinyURL to replace some of our dynamic encrypted keys. Can you advise on whether TinyURL can achieve the following

    1. At present we sell a software online that generates an email. This email contains a link that consists of up to 100 characters (encrypted – order – email etc included in the string) that when clicked will direct to an installation page and carry through the encrypted data needed.

    2. The problem we have is that a small number of people are not able to view the button (click here) that starts the automatic installation. The other problem is that a small number of people risk because of the string size not properly copy and paste the full licence key (installation URL)

    3. What I am thinking is that “In Theory” we can convert the URL installation (which contains hash characters MD5 and dynamic data) into a Tiny URL. The alternate text for the button would then appear as the TinyURL and therefore those people not able to see the button will not view a lengthy (and sometimes scary string) instead seeing a friendly – short – TinyURL.

    4. The end result is that then customers can copy and paste the Tiny URL (easier than previous URL) and then in a new browser easily start the installation process.

    So the keywords here are “In Theory” and it would be appreciated if someone could give me feedback on whether integrating the TinyURL API would in fact create function TinyURL for our Installation URL’s

    Thanks in advance…..

  8. Comment #8 - Permalink
    Tim
    12/09/2009

    Hey Richard -
    Thanks for saving me the time of scouring the web for this info!

    I am having a small-ish problem with permissions. My client has a hosting account with 1and1.com (VPS). When I run your script, I am getting the following errors, which I assume have to do with server settings:

    1. failed to open stream: no suitable wrapper could be found
    2. file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration

    Any thoughts?
    Cheers,
    Tim

  9. Comment #9 - Permalink
    Tim
    12/09/2009

    Hey Richard-
    Update on my above post. I found another method using CURL feature in PHP instead of get_file_contents. Here it is for your readers’ entertainment!

    Of course, I am using this to add a tinyurl to a Twitter post notifying followers of a new article on my client’s website. So coupling this with Twitter’s totally simple API, I am able to do this!

    Thanks for the article, it ultimately pointed me in the correct destination for my specific purposes!

    Tim

  10. Comment #10 - Permalink
    Tim
    12/09/2009

    // you must be stripping tags!

    //gets the data from a URL
    function get_tiny_url($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,’http://tinyurl.com/api-create.php?url=’.$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

    //test it out!
    $new_url = get_tiny_url(‘http://mbnusa.biz/articles.php?aid=68046caa4c-20091125123848&CatID=3&SubCatID=2′);

    //returns http://tinyurl.com/65gqpp
    echo $new_url

  11. Comment #11 - Permalink
    Richard
    12/12/2009

    @Tim, Thanks for posting! I’m happy you found a solution :)

  12. Comment #12 - Permalink
    Richard
    12/12/2009

    @Tony,

    I believe it is possible. There is only 1 way to find out and that’s to give it a try. Go their website (http://tinyurl.com/) and give it a shot. Another popular UR Shortening service is (http://bit.ly/)

  13. Comment #13 - Permalink
    giulio pons
    12/29/2009

    I’ve made a function to convert back the short url into the long url:
    http://www.barattalo.it/2009/12/29/tiny-url-encode-and-decode-with-php/
    bye!
    Giulio

  14. Comment #14 - Permalink
    Richard
    02/16/2010

    @Giulio, Cool! Thanks for sharing that!

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top