Question

Im trying to get URL of final website im getting redirected. Link i go is- http://www.primewire.ag/external.php?url=aHR0cDovL3d3dy5wcm9tcHRmaWxlLmNvbS9sL0RBMTU1RjYwRUYtRkMyNzREQ0Q4RA

after you go that link you get redirected into- http://www.promptfile.com/l/DA155F60EF-FC274DCD8D

I want to get second URL. I found similar question in here

But using code that i got from there

        $url= 'http://www.primewire.ag/external.php?url=aHR0cDovL3d3dy5wcm9tcHRmaWxlLmNvbS9sL0RBMTU1RjYwRUYtRkMyNzREQ0Q4RA';
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $a = curl_exec($ch); // $a will contain all headers
        
        $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
        
        // Uncomment to see all headers
        /*
        echo "<pre>";
        print_r($a);echo"<br>";
        echo "</pre>";
        */
        
        echo $url; // Voila

echos out first URL not redirected URL. Thank you

Was it helpful?

Solution

The server is not redirecting (response is 200 not 304).

The returned HTML contains

<frameset rows="24,100%" frameborder="0">
   <frame src="frame_header.php?hello=&title=" scrolling="no" />
       <frame src="http://www.promptfile.com/l/DA155F60EF-FC274DCD8D" />

</frameset><noframes>http://www.promptfile.com/l/DA155F60EF-FC274DCD8D</noframes>

which would trigger an HTML browser to load that page. curl doesn't parse the returned HTML, just the HTTP headers because it's an HTTP client, not an actual HTML browser. So the second URL is not actually ever being requested (which you could verify by looking at what curl actually returned in your example code), and the effective URL is actually the one you are getting back.

OTHER TIPS

Your code is working right, but the target server is not redirecting you anywhere. Here's the html response to your first curl request:

<frameset rows="24,100%" frameborder="0">
   <frame src="frame_header.php?hello=&title=" scrolling="no" />
       <frame src="http://www.promptfile.com/l/DA155F60EF-FC274DCD8D" />

</frameset><noframes>http://www.promptfile.com/l/DA155F60EF-FC274DCD8D</noframes>
</html>

As you can see, that is not a redirect, it's a frame which loads the final page. Follow redirects won't help you here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top