Papermashup

Subscribe


Tweets


"RT @danbarker: Strangely I saw a really shifty guy drop his wallet earlier today in London. When I handed it back he said "Let me give you…"

@ashleyford 7 hours ago

"RT @McDonaldsCorp: Black Friday **** Need copy and link****"

@ashleyford 7 hours ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

PHP CURL & Tiny URL

AshleyAshley

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

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 9
  • William Eaton
    Posted on

    William Eaton William Eaton

    Reply Author

    I had serious problems one day with the service being slow and basically stopping my website completely. I have updated the code so this doesn’t happen and so that someone else doesn’t lose their day trying to find out why…


    /**
    * Generate short URL using tinymce service
    * @param string $url
    * @return string shortened URL
    */
    function fetchTinyUrl($url) {
    $ch = curl_init();
    $timeout = 1;
    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);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    if($data == '')
    return $url;
    return ''.$data.'';
    }

    Note I have also added code to return the passed URL if it fails so that it doesn’t return nothing at all.


  • Syamsul
    Posted on

    Syamsul Syamsul

    Reply Author

    Hi,

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

    Pls reply to [email protected]

    Tks,
    Syamsul


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

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


  • dan
    Posted on

    dan dan

    Reply Author

    Hi,
    Can you give me an example for:

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


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @dan i wrote this tutorial a while back, you might be best looking at the bit.ly api which has full documentation, you could probably still use bits of this code but it will give you a lot shorter urls. http://bit.ly/apidocs


  • Adeleke
    Posted on

    Adeleke Adeleke

    Reply Author

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


  • Native B
    Posted on

    Native B Native B

    Reply Author

    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