Skip to content

Instantly share code, notes, and snippets.

@sudar
Created September 12, 2011 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudar/1211536 to your computer and use it in GitHub Desktop.
Save sudar/1211536 to your computer and use it in GitHub Desktop.
<?php
/**
* Expand short urls
*
* @param <string> $url - Short url
* @return <string> - Longer version of the short url
*/
function expand_url($url){
//Get response headers
$response = get_headers($url, 1);
//Get the location property of the response header. If failure, return original url
if (array_key_exists('Location', $response)) {
$location = $response["Location"];
if (is_array($location)) {
// t.co gives Location as an array
return expand_url($location[count($location) - 1]);
} else {
return expand_url($location);
}
}
return $url;
}
/**
* Test Short urls
*/
function test_expand_url($short_url, $expected_long_url) {
$actual_long_url = expand_url($short_url);
if ($actual_long_url == $expected_long_url) {
return "Pass for $short_url <br>";
} else {
return "Fail for $short_url <br>";
}
}
// Run testcases
echo test_expand_url('http://bit.ly/J8ytTI', 'http://warpspire.com/talks/chooseyouradventure/?utm_source=twitterfeed&utm_medium=twitter');
echo test_expand_url('http://t.co/hyK0qQCR', 'http://warpspire.com/talks/chooseyouradventure/?utm_source=twitterfeed&utm_medium=twitter');
echo test_expand_url('http://sudarmuthu.com', 'http://sudarmuthu.com');
?>
@itayganor
Copy link

Nice! Is there a cURL version for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment