There are a ton of services available to shorten URLs and one of the most well known, and easiest to use, is TinyURL. With a single line of code, you can fetch a shortened URL from their API using PHP.
TinyURL API:
http://tinyurl.com/api-create.php?url=<URL HERE>
When this is called, it will echo a single line of content:
http://tinyurl.com/cdrh83f
If unsuccessful, you will see:
Fetching with PHP:
file_get_contents('http://tinyurl.com/api-create.php?url=https://www.seoclerks.com');
Creating a function using PHP and the API:
<?phpfunction make_tiny
($url){ return file_get_contents('http://tinyurl.com/api-create.php?url=' . $url);}echo make_tiny
('https://www.seoclerks.com/');?>
cURL Tiny Fetch
If, for whatever reason, you want to fetch with cURL (maybe to use proxies), here is a cURL based function:
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_RETURNTRANSFER
,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT
,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; }echo make_tiny
('https://www.seoclerks.com');