2011-06-28 60 views

回答

5

你可以給unshorten.me一個去。它有一個API

JSON

http://api.unshort.me/?r=http://theshorturl.ly/28292&t=json

會給你:

{ 
    "requestedURL":"http://theshorturl.ly/28292", 
    "success":"true", 
    "resolvedURL":"http://thefullurl.com/a-webiste/what-a-long-url" 
} 
+2

unshorten.me被關停值值他們的API,他們自己的成功的受害者。 (無法承擔每天600萬API調用的託管成本) – Shewfig

+0

我爲此構建了一個小應用程序。退房www.find-t.co。 –

+1

unshorten.me網站現在不見了。 –

6

如果你想在命令行做,捲曲的詳細選項來救援:

curl -v <url> 

爲您提供HTTP答覆。對於t.co,它似乎給你一個HTTP/301的回覆(永久移動)。然後,有一個位置字段,指向縮短後的URL。

+2

也curl -I 會給你的標題信息 – minaz

18

我會遠離您無法控制的外部API。這將簡單地向您的應用程序引入依賴項,這是潛在的失敗點,並且可能會花費您的資金使用。

CURL可以很好地做到這一點。以下是我在PHP做的:

function unshorten_url($url) { 
    $ch = curl_init($url); 
    curl_setopt_array($ch, array(
    CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce 
    CURLOPT_RETURNTRANSFER => TRUE, 
    CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors 
    CURLOPT_SSL_VERIFYPEER => FALSE, 
)); 
    curl_exec($ch); 
    return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
} 

我敢肯定,這可以適用於其他語言,甚至與UNIXy系統curl命令腳本。

http://jonathonhill.net/2012-05-18/unshorten-urls-with-php-and-curl/

+3

尼斯解決方案。我可以建議設置'CURLOPT_NOBODY => true',這樣就可以執行HEAD請求,而最終的資源實際上不會被提取? – MaxArt

+0

這裏的等價命令行是什麼? –

+3

不是一個混蛋,而是RTFM。 http://curl.haxx.se/docs/manpage.html –

6

curl -s -o /dev/null --head -w "%{url_effective}\n" -L "https://t.co/6e7LFNBv"

  • --head-I僅下載的HTTP標頭
  • -w--write-out打印輸出後的指定字符串
  • -L--location如下位置標頭
4

這是一個Python解決方案。

import urllib2 

class HeadRequest(urllib2.Request): 
    def get_method(self): return "HEAD" 

def get_real(url): 
    res = urllib2.urlopen(HeadRequest(url)) 
    return res.geturl() 

測試與實際Twitter的t.co鏈接:

url = "http://t.co/yla4TZys" 
expanded = get_real(url) 

擴大= http://twitter.com/shanselman/status/276958062156320768/photo/1

與一試,只是把它包起來,你是好去。

+0

它不適用於'http://t.co/OFlTpTzCqt'.throws HTTPError:HTTP錯誤303:HTTP服務器返回一個重定向錯誤會導致無限循環。 最近的30x錯誤信息是: 查看其他' – Moj

2

另一個Python溶液,此時依靠請求模塊代替的urllib2(和這些庫的所有其餘部分)上:

#!/usr/bin/env python 

import requests 

shorturl = raw_input("Enter the shortened URL in its entirety: ") 
r = requests.get(shorturl) 

print(""" 
The shortened URL forwards to: 

    %s 
""" % r.url) 
1

這裏是R溶液,從其他的答案移植在此線程,和從example() RCurl包裝的代碼:

unshorten_url <- function(uri){ 
     require(RCurl) 
     if(RCurl::url.exists(uri)){ 
       # listCurlOptions() 
       opts <- list(
         followlocation = TRUE, # resolve redirects 
         ssl.verifyhost = FALSE, # suppress certain SSL errors 
         ssl.verifypeer = FALSE, 
         nobody = TRUE, # perform HEAD request 
         verbose = FALSE 
       ); 
       curlhandle = getCurlHandle(.opts = opts) 
       getURL(uri, curl = curlhandle) 
       info <- getCurlInfo(curlhandle) 
       rm(curlhandle) # release the curlhandle! 
       info$effective.url 
     } else { 
       # just return the url as-is 
       uri 
     } 
} 
0

Twitter擴展了URL。假設您使用twitter API編碼爲json文件的單個推文 。

import json 
urlInfo=[] 

tweet=json.loads(tweet) 
keyList=tweet.keys() # list of all posssible keys 
tweet['entities'] # gives us values linked to entities 

可以觀察到,有一個叫「網址」 鳴叫[「實體」] [「網址」]#給出映射到按鍵的URL

urlInfo=tweet['entities']['expanded_url'] # move it to a list 
# iterating over the list.. gives shortened URL 
# and expanded URL 
for item in urlInfo: 
    if "url" and "expanded_url" in urlInfo.keys(): 
    print(item["url"] + " "+item["expanded_url"])