30

给定一个 t.co 链接,我如何找到链接解析的位置?例如,如果我有 t.co/foo,我想要一个返回 domain.com/bar 的函数或进程。

4

8 回答 8

20

我会远离你无法控制的外部 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);
}

我确信这可以适应其他语言,甚至可以使用curlUNIXy 系统上的命令编写脚本。

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

于 2012-05-19T00:12:43.177 回答
8

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

  • --head-I仅下载 HTTP 标头
  • -w--write-out在输出后打印指定的字符串
  • -L--location遵循位置标题
于 2012-07-04T09:28:37.657 回答
7

如果您想从命令行执行此操作,curl 的详细选项会派上用场:

curl -v <url>

给你 HTTP 回复。对于 t.co,它似乎会给您一个 HTTP/301 回复(永久移动)。然后,有一个 Location 字段,它指向缩短后的 URL。

于 2011-06-28T01:50:07.470 回答
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

用 try-except 结束它,你就可以开始了。

于 2012-12-07T09:15:18.970 回答
2

另一个 Python 解决方案,这次依赖于 requests 模块而不是 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)
于 2013-09-21T22:30:36.760 回答
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 

您可以观察到有一个名为 'urls' 的值 tweet['entities']['urls'] # 给出映射到键 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"])
于 2016-03-25T16:35:32.603 回答
0

你可以试试这个 Java 代码。这是使用 java 拥有的 HttpURLConnection 的代码。: http ://www.srccodes.com/p/article/37/expand-shortened-link-using-java?fb_action_ids=1544985322486585&fb_action_types=og.likes

这个 URL 扩展器将如何工作?将 HttpURLConnection 设为缩短的 url(比如http://goo.gl/WT6eFw)。

提取 HTTP 标头字段“位置”的值。而这个值只不过是扩展的或实际的目标 URL。

关闭连接。

于 2015-12-26T05:47:51.390 回答
0

这是一个 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
        }
}
于 2015-12-20T17:54:17.167 回答