Return to Snippet

Revision: 22039
at December 29, 2009 11:58 by ginoplusio


Initial Code
function doShortURL($url) {
	$short_url= file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode( $url ) );
	return $short_url;
}


function doShortURLDecode($url) {
	$ch = @curl_init($url);
	@curl_setopt($ch, CURLOPT_HEADER, TRUE);
	@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
	@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$response = @curl_exec($ch);
	preg_match('/Location: (.*)\n/', $response, $a);
	if (!isset($a[1])) return $url;
	return $a[1];
}

Initial URL
http://www.barattalo.it/2009/12/29/tiny-url-encode-and-decode-with-php/

Initial Description
The first function make short urls.
The second function decode a short url with CURL. It gets the http header of the short url page. If the header contains a "Location:" header, then it's a redirect, and the decoded url is the url in the "Location" header string, here is the code:

Initial Title
PHP tiny url encode and decode functions

Initial Tags
curl, url

Initial Language
PHP