Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created June 10, 2012 23:30
Show Gist options
  • Save MikeRogers0/2907695 to your computer and use it in GitHub Desktop.
Save MikeRogers0/2907695 to your computer and use it in GitHub Desktop.
URL Shortening Function
<?php
function shorten_url($url, $service=NULL){
if($service== 'tinyurl'){return get_link('http://tinyurl.com/api-create.php?url='.urlencode($url));}
elseif($service == 'urly'){return get_link('http://ur.ly/new.txt?href='.urlencode($url));}
elseif($service == 'isgd'){return get_link('http://is.gd/api.php?longurl='.urlencode($url));}
elseif($service == 'klam'){return get_link('http://kl.am/api/shorten/?format=text&url='.urlencode($url));}
elseif($service == 'unu'){return get_link('http://u.nu/unu-api-simple?url='.urlencode($url));}
else{return get_link('http://api.tr.im/v1/trim_simple?url='.urlencode($url));}
return $url;
}
function get_link($url){
if(function_exists('curl_init')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
if ($resultArray['http_code'] == 200){
return $result;
} else {
return FALSE;
}
curl_close($ch);
}elseif(ini_get('allow_url_fopen') == 1){
return fopen($url, 'r');
}
return FALSE;
}
/* Usage */
echo shorten_url('http://www.fullondesign.co.uk/');
// returns: http://tr.im/JPCz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment