Find where a t.co link goes to

2019-01-11 01:06发布

Given a t.co link, how can I find see where the link resolves? For example, if I have t.co/foo, I want a function or process that returns domain.com/bar.

9条回答
成全新的幸福
2楼-- · 2019-01-11 01:42

I would stay away from external APIs over which you have no control. That will simply introduce a dependency into your application that is a potential point of failure, and could cost you money to use.

CURL can do this quite nicely. Here's how I did it in 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);
}

I'm sure this could be adapted to other languages or even scripted with the curl command on UNIXy systems.

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

查看更多
狗以群分
3楼-- · 2019-01-11 01:44

If you want to do it from the command line, curl's verbose option comes to the rescue:

curl -v <url>

gives you the HTTP reply. For t.co it seems to give you an HTTP/301 reply (permanently moved). Then, there's a Location field, which points to the URL behind the shortened one.

查看更多
做自己的国王
4楼-- · 2019-01-11 01:47

Twitter expands the URL. Assume you have a single tweet using twitter API encoded as json file.

import json
urlInfo=[]

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

You can observe that there is a value called 'urls' tweet['entities']['urls'] # gives values mapped to key urls

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"])
查看更多
登录 后发表回答