RSS Feed
2012/02/04

Tiny URLs mit PHP und der tinyurl API

Max Girkens, 2009/10/15 13:38

für den Fall dass ich nicht der einzige bin, der das bis vorhin nicht wusste:

tinyurl.com hat auch eine API.
Die ist zwar unglaublich simpel, aber das ist doch auch mal schön.

$tinyURL =  file_get_contents( 'http://tinyurl.com/api-create.php?url='.$tooLongURL );

und vice versa (hackish):

function reverse_tinyurl($url){
            // Resolves a TinyURL.com encoded url to it's source.
            $url = explode('.com/', $url);
            $url = 'http://preview.tinyurl.com/'.$url[1];
            $preview = file_get_contents($url);
            preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
            return $matches[1];
        }

und noch die CURL variante von ersterem (via davidwalsh)


//gets the data from a URL
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;
}

One Response to “Tiny URLs mit PHP und der tinyurl API”

  1. Michael says:

    Perfekt :-) Danke!

Leave a Reply