RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.49% (5.98%*) • Fixed: 5.49% (5.98%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.79% (6.41%*) • Investment PI: 5.69% (6.55%*)

Resolve Short URLs To Their Destination URL with PHP

Resolve Short URLs To Their Destination URL with PHP

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.

Note: The fat.ly URL used throughout this page is no longer actively used (parts of the service mimic our shor.tt service).

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.

1
echo beliefmedia_resolve_url($url, $follow = true);

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

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

[ph text="1"p]echo beliefmedia_resolve_url($url, $follow = false);[/php]

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

To return all the redirections in an array, use:

1
beliefmedia_resolve_url_array($url)

For the purpose of printing our example we'll use the following:

1
$url = 'http://fat.ly/redirectdemo';
2
echo '
3
<pre>' . print_r(beliefmedia_resolve_url_array($url), true) . '</pre>
4
 
5
';

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.

1
<?php 
2
/*
3
 Get Redirect URL from Header String
4
 Resolve Short URLs To Their Destination URL with PHP
5
 http://www.beliefmedia.com/resolve-short-url
6
*/
7
 
8
function beliefmedia_get_url_string($headers) {
9
  preg_match('/Location:\s?(\bhttps?:\/\/[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))?/si', $headers, $match);
10
  $url = rtrim($match['1'], '/');
11
 return $url;
12
}

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).

1
<?php 
2
/*
3
 Resolve the queried URL
4
 Resolve Short URLs To Their Destination URL with PHP
5
 http://www.beliefmedia.com/resolve-short-url
6
*/
7
 
8
function beliefmedia_resolve_url($url, $follow = true) {
9
 
10
  $ch = curl_init($url);
11
  curl_setopt($ch, CURLOPT_HEADER, true);
12
  curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
13
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
14
  curl_setopt($ch, CURLOPT_NOBODY, true);
15
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
16
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow);
17
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
18
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
19
  $headers = curl_exec($ch);
20
  if ($follow) $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
21
  curl_close($ch);
22
 
23
  /* If we're following the URL, we'll fetch the redirect URL from header string */
24
  $result = ($follow) ? $redirect_url : internoetics_get_url_string($headers);
25
 
26
 return $result;
27
}

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

1
<?php 
2
/*
3
 Takes a URL and returns the redirections in an array
4
 Resolve Short URLs To Their Destination URL with PHP
5
 http://www.beliefmedia.com/resolve-short-url
6
*/
7
 
8
function beliefmedia_resolve_url_array($url) {
9
 $finalurl = rtrim(beliefmedia_resolve_url($url, $follow = true), '/');
10
 $domainarray[] = $url;
11
  while ($finalurl != $url) {
12
    $url = rtrim(beliefmedia_resolve_url($url, $follow = false), '/');
13
    $domainarray[] = $url;
14
  }
15
 return $domainarray;
16
}

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 .

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.

1
<?php 
2
/*
3
 Print the Location Redirections in a list
4
 Resolve Short URLs To Their Destination URL with PHP
5
 http://www.beliefmedia.com/resolve-short-url
6
*/
7
 
8
function beliefmedia_resolve_urls($url, $array = false) {
9
  $getheaders = get_headers($url, 1);
10
  $locations = (isset($getheaders['Location'])) ? $locations = $getheaders['Location'] : array('0' => 'Unavailable');
11
 
12
  /* Add first URL to array */
13
  array_unshift($locations, $url);
14
 
15
  if ($array === false) foreach ($locations AS $location => $uri) $html .= '<li>' . $uri . '</li>';
16
  else $html = (array) $locations;
17
 
18
 /* Return list or array */
19
 return ($array === false) ? '<ul> ' . $html . '</ul>' : $locations;
20
}
21
 
22
/* Usage */
23
$url = 'http://fat.ly/redirectdemo';
24
echo beliefmedia_resolve_urls($url);

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:

1
<?php 
2
/*
3
 Get destination URL using get_headers()
4
 Resolve Short URLs To Their Destination URL with PHP
5
 http://www.beliefmedia.com/resolve-short-url
6
*/
7
 
8
$url = 'http://fat.ly/redirectdemo';
9
$url_array = beliefmedia_resolve_urls($url, $array = true);
10
echo 'Final redirect is ' . end($url_array);

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.
1
<?php 
2
/* Wildcard Array Search */
3
function beliefmedia_inarray_wildcard ( $needle, $arr ) {
4
  return array_values( preg_grep( '/' . str_replace( '*', '.*', $needle ) . '/', $arr ) );
5
}
6
 
7
/* Usage */
8
$matchesarray = beliefmedia_inarray_wildcard('Location', $w);
  • 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:
1
<?php 
2
/* Find all links */
3
preg_match_all(&quot;/(http|https|ftp):\/\/[^<>[:space:]]+[[:alnum:]#?\/&=+%_]/&quot;, $data, $match);
4
$list = $match[0];
  • 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 • Version 0.2, 1.3K, zip, Category: PHP Code & Snippets

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment