and like us on FB

This article will provide a few PHP solutions to determine a destination URL from a link. With the proliferation of short URL services, it’s become important to identify how a URL will ultimately resolve… and what course it takes en-route to its final destination. We’ll look at two methods: a CURL request, and PHP’s get_headers() function. Both functions on this page will permit you to determine the first redirect or the final destination – or print all the redirections in an array.

1. Making a CURL Request

Consider the short URL of fat.ly/redirectdemo . The fat.ly URL will redirect to flight.org … but not before it passes through bit.ly , tinyurl , and shor.tt .

So, to determine the final destination of our first fat.ly link, we will use the following function.

Returns: http://www.flight.org.

To print the first redirect that takes place, we would use:

Returns: http://bit.ly/1Se6mph.

To return all the redirections in an array, use:

For the purpose of printing our example we’ll use the following (make sure you add the correct pre tags; removed for formatting):

The result:

Resolve Short URLs To Their Destination URL with PHP

The code is divided up into three functions.

The first function takes the header (string) response of a CURL request and (using preg_match) extracts the Location redirect URL.

The second function is the heart of our functionality. Given a URL, a request is made to the destination website. Depending upon the Boolean value of $follow we either make an attempt to resolve the URL to the destination, or simply determine the location of the next redirection.

The CURLOPT_FOLLOWLOCATION option is what determines if the request should follow the Location redirect in the header. If true it’ll follow the Location header – false if otherwise. The CURLOPT_NOBODY option is used to exclude the body from the output (including only headers).

The final function will take a $url and loop through the redirections and add them to the $domainarray.

2. Making a Request with PHP’s get_headers()

To resolve a URL and/or print all the redirections that take place, you may optionally use PHP’s get_headers() function – a forced option if the CURL library isn’t installed on your server. It’s a shorter option than the CURL option, and because the resulting array is returned natively by the function we’ll mitigate the risks associated with the regular expression that recursively determines the redirect .

Resolve Short URLs To Their Destination URL with PHP

Data returned in an array by PHP

Note the absence of our first fat.ly link. We use array_unshift to add this to the top of the array after we query the result. We then simply loop over the data as we did previously and print the result to a variable.

The PHP function to print the redirections in a list is as follows.

You may optionally return the result in an array using $url_array = beliefmedia_resolve_urls($url, $array = true);. To find the final redirect, use the following:

Considerations

  • We originally used this code to resolve short URLs on our Usenet website. We could then scrutinize the destination website for spam or malware. Additionally, we could provide information on the destination page.
  • An scheduled article will retrieve meta and object graph tags from the destination website. We use this for the preview features of our short URL services.
  • In a previous version of this code we checked the header for the ‘Location‘ value that took on a slippery position in the header array. The following code is an alternative to the regex on the entire returned header (via CURL). The function returns an array with a single value.

  • The above function returns an array with a single matching Location: * result. If empty(), bad… if not, the result is $matchesarray['0'];.
  • In an older version of this article (published on the retired Internoetics), the focus was on batch-resolving all URLs on a page. We used the following to populate an array with URL data:

  • With the above $list result, we compared the URL root domains against an array of known shorteners before resolving them. This method has become a little redundant because there’s just too many shorteners to keep track of. If you only wanted to resolve certain URLs, though, it would still serve a purpose.
  • Because we have a few sites that require this feature, and because we also extract meta data, we built a little centralized API to consolidate the resolved data. Our clients have access if they require it.

Download

Title: Resolve Short URLs To Their Destination URL with PHP
Description: Resolve Short URLs To Their Destination URL with PHP. Curl and get_headers() examples.
Download: PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/1q4R