Monday, August 9, 2010

Create a Tiny URL using PHP

Function to create a tiny URL for the site using PHP....


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

$new_url = get_tiny_url('http://anilkajadoo.blogspot.com/');

//The tinyURL has been generated & just print it below
echo $new_url;


       

No comments: