0

I need to retrieve the redirect URL from a link, but when I pull the header information it just returns:

Locations: redirecting...

Afterwards, the page is redirected to the URL I want to redeem. I'm using the code:

$url = 'http://168.0.51.253:8000/boleto/74140-P2UTT5T4IH/';

// Final URL I want to retrieve = https://data.galaxpay.com.br/prooverisp/boleto/202109YB0K77L7WYP5PPY0NGT6DMKOD01155936

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
curl_close($ch);

$headers = explode("\n", $a);
$j = count($headers);
$ticket = null;
for ($i = 0; $i < $j; $i++) {
    if (strpos($headers[$i], "Location:") !== false) {
        $ticket = trim(str_replace("Location:", "", $headers[$i]));
        break;
    }
}

var_dump($headers);

How can I wait for the redirect and retrieve the final URL? I work with PHP.

  • 1
    Is `Locations: redirecting...` what the `var_dump` at the bottom shows? Barely can believe that because a) the header is called "Location" (singular) and b) it has to have a target location to be understood by the browser. – ArSeN Sep 02 '21 at 11:07
  • Does this answer your question? [How can I find where I will be redirected using cURL?](https://stackoverflow.com/questions/3519939/how-can-i-find-where-i-will-be-redirected-using-curl) – AD7six Sep 02 '21 at 11:16

1 Answers1

1

You are trying to grab a redirection (aka Location) header from the first link, but when you open it with JavaScript disabled in your browser you will see that there is none. In fact, you always get a response code 200 and no Location header.

There actually is a javascript that instantly redirects you to the target URL:

<!doctype html>
<style type="text/css" media="all">
html, body {
    height: 100%;
    margin: 0;         /* Reset default margin on the body element */
}
iframe {
    display: block;       /* iframes are inline by default */
    border: none;         /* Reset default border */
    width: 100%;
    height: 100%;
}
</style>
Redirecionando... 
<script type="text/javascript">
document.location.href = "XYZ"
</script>

XYZ= Target link you want to grab, I just removed it here for privacy reasons.

You can grab it like so:

$url = 'http://1.2.3.4:8000/foo/bar/'; // URL removed for privacy

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
curl_close($ch);

$lines = explode("\n", $a);
$j = count($lines);
$ticket = null;
for ($i = 0; $i < $j; $i++) {
    if (preg_match('/document.location.href = \"(.*)\"/', $lines[$i], $matches)) {
        $ticket = trim($matches[1]);
        break;
    }
}

var_dump($ticket);

Also, your array looping is kind of complicated for no reason, so you can simplify it by using a foreach:

$ticket = null;
foreach (explode("\n", $a) as $line) {
    if (preg_match('/document.location.href = \"(.*)\"/', $line, $matches)) {
        $ticket = trim($matches[1]);
        break;
    }
}
ArSeN
  • 5,133
  • 3
  • 19
  • 26