Adam Mc
Adam Mc

Reputation: 75

How can I determine the final link of url shortned link without loading within the page?

I have links of messages coming in, say in an email message body. The issue right now is I want to click on the content and be able to display the unwrapped link in the right message pane (I use jQuery).

I am using jQuery and figured that using AJAX to call another PHP script to send the requests to bit.ly, etc. would work. I am not 100% sure how to do this.

The main issue is that some of these links are 2-3 times shortened as Twitter has their own automatic shortner. Any help on this would be appreciated.

Thanks.

Upvotes: 3

Views: 154

Answers (1)

Ayman Safadi
Ayman Safadi

Reputation: 11552

You could use a service called LongURL. The API call in jQuery would look something like this:

$.ajax({
    url: 'http://api.longurl.org/v2/expand?format=json&url=http%3A%2F%2Fbit.ly%2Fv20RLs',
    dataType: 'jsonp',
    method: 'get',
    success: function(response) {
        alert(response['long-url']);
    }
});

or

$.ajax({
    url: 'http://api.longurl.org/v2/expand',
    data: {
        format: 'json',
        url: 'http://bit.ly/v20RLs'
    },
    dataType: 'jsonp',
    method: 'get',
    success: function(response) {
        alert(response['long-url']);
    }
});

Upvotes: 3

Related Questions