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.

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

und vice versa (hackish):

1
2
3
4
5
6
7
8
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)

1
2
3
4
5
6
7
8
9
10
11
12
//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 Reply to “Tiny URLs mit PHP und der tinyurl API”

Comments are closed.