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.
1 2 3 4 5 6 |
<?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.
1 |
<?php echo(createTinyUrl('https://www.richardcastera.com/2009/05/09/creating-a-tinyurl-with-tinyurl-api/')); ?> |
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 😉
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.
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
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…..
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
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
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
Hi, with your help, I create an App based on the API and Flex, see it: http://tinyurl.com/33cjdsr
hi,
There is any api for posting the image in twitter using tinyurl in iphone development
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
Thank you! I just made a iMacros script with the API. 🙂
Sadly this method seems to have stopped working for me. I’ve found others that do work. Strange that your one no longer does 🙁 or is it just me?
Working, But sometimes it throws 400 Bad Request error.
Aw, this was an extremely nice post. Taking the time and actual effort to
produce a good article… but what can I say… I hesitate a whole lot and never manage to get nearly anything done.