1

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?

1
  • Turn off the curl option to auto-follow redirects, then extract the Location header from the initial response headers. Commented Apr 25, 2012 at 17:10

3 Answers 3

1

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;
        }
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

probably the getAllRedirects function should be started ..if so unfortunately it return compredia.eu/… .. the $url and not the final url compredia.eu/… ... sad..
@user1356820 Have you tried using getFinalRedirect? That's the one you should try. ;-)
@user1356820 Could you please confirm something (I think found the solution for you) : you have this (compredia.eu/…) and you want to get that (compredia.eu/…), right?
@user1356820 Check my other answer - I think I did it :-)
0
$ch = curl_init($url);
//set options
curl_exec($ch);
$lasturl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

2 Comments

Have tried it already return value : Ink & Toner Finder: echo $lasturl
have tried and turned off the curl option to auto-follow redirects. Happens the same I can not get the final urls name.Just an Ink & Toner Finder messages cames out and the page automatically redirects to compredia.eu/… .. Looking at the code there maybe an ajax search / result ? the option &mode=finder triggers the page to redirect because without it does nothing . So if there is a solutions I do want to have in a variable the name = compredia.eu/…
0

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

1 Comment

what if not redirected by javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.