Get final URL from double shortened URL (t.co & # 8594; bit.ly & # 8594; final)

I was not able to convert the double shortened url to expanded url successfully using the following function I got from here :

function doShortURLDecode($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);
        $response = @curl_exec($ch);
        preg_match('/Location: (.*)\n/', $response, $a);
        if (!isset($a[1])) return $url;
        return $a[1];
    }

      

I was having problems where the expanded url I received was again a shortened url that has an expanded url.

How do I get the final extended URL after it has been launched through URL shortening services?

+3


source to share


4 answers


Finally found a way to get the final url of the double shortened url. The best way is to use the longurl api for it.

I'm not sure if this is the correct way, but I finally get the output as the final url :)

Here's what I did:

<?php
 function TextAfterTag($input, $tag)
 {
        $result = '';
        $tagPos = strpos($input, $tag);

        if (!($tagPos === false))
        {
                $length = strlen($input);
                $substrLength = $length - $tagPos + 1;
                $result = substr($input, $tagPos + 1, $substrLength); 
        }

        return trim($result);
 }

 function expandUrlLongApi($url)
 {
        $format = 'json';
        $api_query = "http://api.longurl.org/v2/expand?" .
                    "url={$url}&response-code=1&format={$format}";
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $api_query );
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $fileContents = curl_exec($ch);
        curl_close($ch);
        $s1=str_replace("{"," ","$fileContents");
        $s2=str_replace("}"," ","$s1");
        $s2=trim($s2);
        $s3=array();
        $s3=explode(",",$s2);
        $s4=TextAfterTag($s3[0],(':'));
        $s4=stripslashes($s4);
        return $s4;
 }
 echo expandUrlLongApi('http://t.co/dd4b3kOz');
?>

      

The output I get is:



"http://changeordie.therepublik.net/?p=371#proliferation"

      

The above code works.

The code @cryptic shared is also correct , but I was not able to get the result on my server (possibly due to some configuration issue).

If anyone thinks this could be done in some other way, please feel free to share it.

+1


source


Since it t.co

uses HTML redirection using JavaScript redirects and / or <meta>

, we need to transfer its content first. Then extract the URL from it bit.ly

to make a request for the HTTP header to get the final location. This method does not rely on server side cURL enablement and uses all native PHP5 functions:

Tested and working!



function large_url($url) 
{
    $data = file_get_contents($url); // t.co uses HTML redirection
    $url = strtok(strstr($data, 'http://bit.ly/'), '"'); // grab bit.ly URL

    stream_context_set_default(array('http' => array('method' => 'HEAD')));
    $headers = get_headers($url, 1); // get HTTP headers

    return (isset($headers['Location'])) // check if Location header set
        ? $headers['Location'] // return Location header value
        : $url; // return bit.ly URL instead
}

// DEMO
$url = 'http://t.co/dd4b3kOz';
echo large_url($url);

      

+1


source


Maybe you should just use CURLOPT_FOLLOWLOCATION

= true and then figure out the final URL you were directed to.

0


source


In case the problem is not Javascript redirect like in t.co it is stackexchange url redistribution like https://stackoverflow.com/q/62317

fine:

public function doShortURLDecode($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);
    $response = @curl_exec($ch);
    $cleanresponse= preg_replace('/[^A-Za-z0-9\- _,.:\n\/]/', '', $response);
    preg_match('/Location: (.*)[\n\r]/', $cleanresponse, $a);
    if (!isset($a[1])) return $url;
    return parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$a[1];
}

      

0


source







All Articles