2010-04-30 42 views

回答

8

我卻沒有意識到bit.ly的API,這裏是原始的方式來做到這一點:

$context = array 
(
    'http' => array 
    (
     'method' => 'GET', 
     'max_redirects' => 1, 
    ), 
); 

@file_get_contents('http://bit.ly/cmUTtb', null, stream_context_create($context)); 

echo 'Redirect to: ' . str_replace('Location: ', '', $http_response_header[6]); 
+1

您的解決方案非常棒,並且不僅適用於bit.ly.謝謝! – SaltLake 2010-04-30 21:53:30

1

使用curl,默認情況下不會遵循重定向。

+0

代碼會讓您的答案最好 – 2013-03-06 17:29:25

6

您可以查詢long.URL的bit.ly的API(documentation)。您將需要您的用戶名和API密鑰(可在account page上找到)。

$endpoint = 'http://api.bit.ly/v3/expand?'; 
$params = array(
    'shortUrl' => 'http://bit.ly/aUmUDq', 
    'login' => 'your_bitly_username', 
    'apiKey' => 'your_api_key', 
    'format' => 'txt' 
); 
$api_url = $endpoint . http_build_query($params); 
echo file_get_contents($api_url);