Find where a t.co link goes to [closed]
Asked Answered
L

7

33

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

Lassitude answered 28/6, 2011 at 1:40 Comment(4)
I'm voting to leave this question closed because it needs focus. It is asking for a function or process but doesn't specify a programming language. There are now many competing answers for a variety of technologies. There is really no way to choose a "best" answer as the answers are for different situations. In addition, this question is attracting poor-quality recommendation answers. (Several such answers have been deleted.)Forerunner
If you paste the url into your browser(a process)'s address bar, it will turn into the link that it resolves to. You could even just... click it.Successor
@StephenOstermiller sounds good. there were some upvoted answers that have since been deleted. But, after 10+ years I have no idea what my use case was anyway.Lassitude
@KevinB Not entirely helpful, as "just click on link" isn't really scalable nor secure. But I'll try that next time!Lassitude
K
22

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/

Kalbli answered 19/5, 2012 at 0:12 Comment(1)
Nice solution. May I suggest to set CURLOPT_NOBODY => true so a HEAD request is performed instead and the final resource isn't actually fetched?Taxiplane
M
8

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.

Melamine answered 28/6, 2011 at 1:50 Comment(3)
Why do you use -v? It outputs a whole pile of info you don't need. curl -I (capital i) would suffice.Wynd
This solution does not follow multiple redirects. Some of the other solutions do, e.g. https://mcmap.net/q/444159/-find-where-a-t-co-link-goes-to-closedWynd
@SybillePeters That linked solution will access the destination server, which might be a problem if e.g used as a link from spam (where it might flag the address as valid) (I got some spam with t.co links)Masterly
P
7

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

  • --head or -I only downloads HTTP headers
  • -w or --write-out prints the specified string after the output
  • -L or --location follows location headers
Pachalic answered 4/7, 2012 at 9:28 Comment(2)
I like this solution. It only outputs the URL (so no further parsing is necessary) and it follows multiple redirects. Effectively, it is the same solution as the one in PHP on the command line, right? https://mcmap.net/q/444159/-find-where-a-t-co-link-goes-to-closedWynd
This would access the destination - if the link is used in spam (which is why I'm here) this will alert them that the email address is valid...Masterly
C
3

Here is a Python solution.

import urllib2

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

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

Tested with an actual twitter t.co link:

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

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

Wrap it up with a try-except and you are good to go.

Commonplace answered 7/12, 2012 at 9:15 Comment(1)
it doesn't work with http://t.co/OFlTpTzCqt.throws HTTPError: HTTP Error 303: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: See OtherCoastwise
M
1

Another Python solution, this time relying on the requests module instead of urllib2 (and all the rest of those libraries):

#!/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)
Mcglothlin answered 21/9, 2013 at 22:30 Comment(1)
Better yet, only download the headers by using: r = requests.head(shorturl, allow_redirects=True)Conformance
C
-1

Here is an R solution, ported from other answers in this thread, and from example() code of the RCurl Package:

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
        }
}
Clinkerbuilt answered 20/12, 2015 at 17:54 Comment(0)
L
-1

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"])
Luigi answered 25/3, 2016 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.