user1356820
user1356820

Reputation: 41

Get redirected url name using curl?

I have the following link:

http://www.compredia.eu/finder_listing.php?cpath=10728,100021,1063355&mode=finder

which automatically redirects to:

http://www.compredia.eu/hp-printcartridges-for-hp-business-inkjet-2800.html

How can I get the url's redirected page, using curl?

Upvotes: 1

Views: 2589

Answers (3)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820


UPDATE

(this is what will work in your case)


Code :

<?php

function curlRedir($url)
{
    $go = curl_init($url);
    curl_setopt ($go, CURLOPT_URL, $url);

    static $curl_loops = 0;
    static $curl_max_loops = 20;

    if ($curl_loops++>= $curl_max_loops)
    {
        $curl_loops = 0;
        return FALSE;
    }

    curl_setopt($go, CURLOPT_HEADER, true);
    curl_setopt($go, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($go);
    $pattern = '/self\.location\.href=\'(.+)\';/';
    preg_match($pattern, $data, $matches);

    curl_close($go);
    return $matches[1];
}

$c = curlRedir("http://www.compredia.eu/finder_listing.php?cpath=10728,100021,1063355&mode=finder");

echo $c;

?>

Output :

http://www.compredia.eu/hp-printcartridges-for-hp-business-inkjet-2800.html

Upvotes: 0

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Here's what I'm using - it should work for you, too :

<?php
function getRedirectUrl($url){ 
        $redirect_url = null;

        $url_parts = @parse_url($url);
        if (!$url_parts) return false;
        if (!isset($url_parts['host'])) return false; //can't process relative URLs
        if (!isset($url_parts['path'])) $url_parts['path'] = '/';

        $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
        if (!$sock) return false;

        $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
        $request .= 'Host: ' . $url_parts['host'] . "\r\n";
        $request .= "Connection: Close\r\n\r\n";
        fwrite($sock, $request);
        $response = '';
        while(!feof($sock)) $response .= fread($sock, 8192);
        fclose($sock);

        if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
                if ( substr($matches[1], 0, 1) == "/" )
                        return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
                else
                        return trim($matches[1]);

        } else {
                return false;
        }

}

function getAllRedirects($url){
        $redirects = array();
        while ($newurl = getRedirectUrl($url)){
                if (in_array($newurl, $redirects)){
                        break;
                }
                $redirects[] = $newurl;
                $url = $newurl;
        }
        return $redirects;
}

function getFinalRedirect($url){
        $redirects = getAllRedirects($url);
        if (count($redirects)>0){
                return array_pop($redirects);
        } else {
                return $url;
        }
}
?>

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

$ch = curl_init($url);
//set options
curl_exec($ch);
$lasturl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

Upvotes: 0

Related Questions