Reputation: 5407
I need to decode tinyurl
coming from tweets,
<?php
$url = "http://t.co/vpDXYaXl7m";
$full = URLDecode($url);
echo "URL is $full";
function URLDecode($url)
{
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$url_resp = @curl_exec($ch);
preg_match('/Location: (.*)\n/', $url_resp, $i);
if (!isset($i[1]))
{
return $url;
}
return $i[1];
}
?>
It gives the following error:
Fatal error: Cannot redeclare URLDecode() in F:\wamp\www\refresh\tinydecode.php
on line 23
What is the issue? Is there any other way to decode this tiny urls?
Do I need any change if I'm using CURL?
Upvotes: 0
Views: 372
Reputation: 76656
urldecode()
is a built-in PHP function. You can't override those function names (well, you can, but not without special hacks). Change your function name to something different, say MyURLDecode
.
function MyURLDecode($url)
{
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$url_resp = @curl_exec($ch);
preg_match('/Location: (.*)\n/', $url_resp, $i);
if (!isset($i[1]))
{
return $url;
}
return $i[1];
}
You might want to change your regular expression to /Location:\s+(.*)\n/i
to account for lower case location
headers and extra white-spaces.
Upvotes: 1