user123
user123

Reputation: 5407

decode twitter dinyurl to get full url link

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

Answers (1)

Amal Murali
Amal Murali

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

Related Questions