我有一個這樣的的tumblr鏈接:http://tumblr.com/XXXXXXXX展開的tumblr縮短鏈接
爲了與他們的API的博客I need the hostname溝通,因此我需要擴大鏈接到完整的鏈接。例如:http://blogname.tumblr.com/post/XXXXXX
如何擴展tumblr縮短的鏈接?
我有一個這樣的的tumblr鏈接:http://tumblr.com/XXXXXXXX展開的tumblr縮短鏈接
爲了與他們的API的博客I need the hostname溝通,因此我需要擴大鏈接到完整的鏈接。例如:http://blogname.tumblr.com/post/XXXXXX
如何擴展tumblr縮短的鏈接?
在Tumblr API中,我不認爲這是可能的,如Derek Gottfrid所述。如果您在應用程序或服務中使用它,則可以嘗試查看標題。
例如,在Python中,你可以使用urllib2
import urllib2
tumb = urllib2.urlopen('http://tumblr.com/XXXXXXXX')
print tumb.url
在PHP中,你可以使用get_headers方法
$url = 'http://tumblr.com/XXXXXXXX'
print_r(get_headers($url))
這裏是另一種方式來做到這一點的紅寶石。它需要遵循tumblr重定向。從http://tmblr.co/XXXXX到http://www.tumblr.com/XXXXX,最後到擴展的URL。從Net::HTTP documentation:
require 'net/http'
require 'uri'
def get_permalink(uri_str, limit=5)
# You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPOK then uri_str
when Net::HTTPMovedPermanently
get_permalink(response['location'], limit-1)
when Net::HTTPFound
get_permalink(response['location'], limit-1)
end
end
希望它可以幫助別人
感謝您的回答。我在線上回答。我可能會檢查後端解決方案,儘管我會爲此提供一個直接的API。 –