PHP CURL & Tiny URL

PHP CURL & Tiny URL

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on March 12, 2009

Here’s how to use PHP CURL and the TinyURL api to generate tiny urls on the fly. This is a similar tutorial to the one I wrote a while back on using the Twitter API

I’ve set the variable $maketiny as the url that you want to shorten. This then gets passed into the function and processed with CURL. CURLOPT_RETURNTRANSFER returns the value of curl_exec($ch); as a string and doesn’t just display it on the screen allowing us to process the data further.

//the url you want to make tiny
$maketiny = 'http://www.papermashup.com';

function make_tiny($url)
{
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch, CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
$tinyurl = make_tiny(''.$maketiny.'');

php echo $tinyurl; ?>

demodownload

More tutorials from Papermashup
Comments
8 discussions around PHP CURL & Tiny URL
  1. Pingback: Andivista Info und Technikblog » Blog Archive » Twitter API

  2. Syamsul says:

    Hi,

    I’m beginer with PHP cURL.
    How to get cURL? or where I can download the source curl?

    Pls reply to wsyakinah@gmail.com

    Tks,
    Syamsul

    • Ashley says:

      @Syamsul CURL is a module that is installed as standard with PHP5 and upwards, if you don’t have server administration access, i.e you can upload software to the server and configure it, then you won’t be able to install CURL, read a few posts up it explains how to find out if you have CURL installed.

  3. dan says:

    Hi,
    Can you give me an example for:

    What if i need to make shorter multiple links at once ?

  4. Adeleke says:

    This is will be a good tool to use, if only if it will work as you explained it here, thank you.

  5. Native B says:

    HI Ashley,

    Could you do an example using web services? like the one at http://101.gs/apiexample.php. By the what happens if CURL is not enabled on the server?

    NB

  6. Pingback: PHP CURL & Tiny URL | Switch on the Code

Leave a Reply

Your email address will not be published. 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>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.