» The only valid measurement of code quality: WTFs/minute « Max Girkens

Tiny URLs mit PHP und der tinyurl API

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;
}
Bookmark and Share

Tags: , ,

Kategorien: PHP

Ein Kommentar

  1. Perfekt :-) Danke!

Kommentieren »