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/'));
?>

Tags:

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

  1. Native B

    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. Richard

    Hey Native B,

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

  3. dan

    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. Richard

    @Dan,

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

  5. Chris

    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. Richard

    @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. Tony

    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. Tim

    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. Tim

    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. Tim

    // 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. Richard

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

  12. Richard

    @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. giulio pons

    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. Richard

    @Giulio, Cool! Thanks for sharing that!

Leave a Reply