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

18 comments for Creating a TinyURL with TinyURL's API

Native B's picture

Great example - could you...

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 Wink

Richard Castera's picture

Hey Native B, Thanks for your...

Hey Native B,

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

dan's picture

Thank you for the...

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.

Richard Castera's picture

Dan, You can create a for...

Dan,

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

Chris's picture

First off, thanks for this...

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

Richard Castera's picture

Chris, Hey so here is how you...

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 Wink

Tony's picture

We are playing around with...

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

Richard Castera's picture

Tony, I believe it is...

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/)

Tim's picture

Hey Richard - Thanks for...

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

Tim's picture

Hey Richard- Update on my...

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

Tim's picture

// you must be stripping...


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

Richard Castera's picture

Tim, Thanks for posting! I'm...

Tim, Thanks for posting! I'm happy you found a solution Smile

giulio pons's picture

I've made a function to...

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

Richard Castera's picture

Giulio, Cool! Thanks for...

Giulio, Cool! Thanks for sharing that!

guoliang li's picture

Hi, with your help, I create...

Hi, with your help, I create an App based on the API and Flex, see it: http://tinyurl.com/33cjdsr

Richard Castera's picture

Great!...

Great!

bharani's picture

hi, There is any api for...

hi,
There is any api for posting the image in twitter using tinyurl in iphone development

Rajat Gupta's picture

How many hits can make in a...

How many hits can make in a day or in 1 hour using tiny url shortenetng api.
i need unlimited url shortener it is possible in tiny url shortening api.

(OR)
is there any other url api available for unlimited ..
please help us
Thaks in advance

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.