1

別の (短縮された) URL が指す URL を取得する方法はありますか?
たとえば、http://www.stackoverflow.com を次の URL に短縮ました: http://tinyurl.com/5b2su2

PHP で次のような関数が必要です。

getTrueURL($shortened_url)
{
 // ?
}

が呼び出された'http://stackoverflow.com'ときに返されるはずです。getTrueURL('http://tinyurl.com/5b2su2')これどうやってするの?

PS: サーバー側でそれが不可能な場合は、JavaScript ソリューションも使用できます。

4

2 に答える 2

2

これが必要だと思います:

<?php
function getTrueURL($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    $data = curl_getinfo($ch);
    return $data["url"];
}

echo getTrueURL("http://tinyurl.com/5b2su2");
?>
于 2013-08-17T11:53:39.177 に答える
0
<?php



function tinyurl_reverse($szAddress)
{
    $szAddress = explode('.com/', $szAddress);
    $szAddress = 'http://preview.tinyurl.com/'.$szAddress[1];
    $szDocument = file_get_contents($szAddress);
    preg_match('~redirecturl" href="(.*)">~i', $szDocument, $aMatches);

    if(isset($aMatches[1]))
    {
        return $aMatches[1];
    }

    return null;
}

echo tinyurl_reverse('http://tinyurl.com/5b2su2');
?>
于 2013-08-17T11:54:07.287 に答える