0
// From URL to get redirected URL
$url = 'https://www.shareasale.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625';
  
$ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
    die("Couldn't initialize a cURL handle");
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL,            $url);
$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);

// execute
$ret = curl_exec($ch);

if (!empty($ret)) {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler
    if (empty($info['http_code'])) {
            die("No HTTP code was returned");
    } else {
        echo 'REDIRECTED FINAL URL'.$info['url']); // this does not give final url.
    }

}

is their any way we can get final url from a url after all re-directions ? Let me know if any changes needs to be done in this code ?

https://www.shareasale.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625

This is the url which has lots of redirections, i am testing code with this one but it does not return final url, it return some the url then the url which you see in url bar.

  • From https://www.php.net/manual/en/function.curl-getinfo.php, does `$info['redirect_url']` give anything different? Or perhaps https://stackoverflow.com/a/28458522/1213708? – Nigel Ren Jul 03 '21 at 06:43
  • $info['redirect_url'] returns blank –  Jul 03 '21 at 06:47
  • stackoverflow.com/a/28458522/1213708 did'nt help –  Jul 03 '21 at 06:51
  • There must be something that can help –  Jul 03 '21 at 06:59
  • @NigelRen What you say ? –  Jul 03 '21 at 06:59
  • https://www.shareasale.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625 This is the url which has lots of redirections, i am testing code with this one but it does not return final url, it return some the url then the url which you see in url bar. –  Jul 03 '21 at 07:05
  • 1
    I've just tried your code and it gives `REDIRECTED FINAL URLhttps://www.shareasale-analytics.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625&shrsl_analytics_sscid=71k5%5F2zo0q&shrsl_analytics_sstid=71k5%5F2zo0q`, which is different to the start url. Not sure what it's supposed to be so not sure what else to do. – Nigel Ren Jul 03 '21 at 07:13
  • @NigelRen is should be the url which we get on url bar when we directly access the url. I found that it uses " window.location.replace('https:\/\/loomyhome.com\/collections\/all-products\/products\/blue-my-mind-rug?sscid=71k5_2zvjg&') " to redirect & this is the final url i,e (loomyhome) –  Jul 03 '21 at 07:25

1 Answers1

0

The code you have is working correctly, but it is only part of what you want. When you get to the final URL redirect, your return includes...

<HTML><head></head><body>

    
        <script LANGUAGE="JavaScript1.2">
        window.location.replace('https:\/\/loomyhome.com\/collections\/all-products\/products\/blue-my-mind-rug?sscid=71k5_300lf&')
        </script>
    

</body></html>

So you then need to extract the URL from there. You can use a regex (not my best skill) which would be something like...

preg_match('#(https:.*?)\'\)#', $ret, $match);

echo stripslashes($match[1]);

(using stripslashes to unescape the string). Gives...

https://loomyhome.com/collections/all-products/products/blue-my-mind-rug?sscid=71k5_3097f&
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55