How to get the final URL after multiple types of redirects?

Viewed 29

what i try to find is the final URL from a local price comparison site, Pricy.ro.

Example URL: https://www.pricy.ro/extensionhtml?url=https://www.emag.ro/telefon-mobil-apple-iphone-12-128gb-5g-black-mgja3rm-a/pd/DZDJ27MBM/

From this i parse the URL's from HTML as an array:

$shops = [
['shop' => 'emag', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60e6d3aec716012740f624a6&source=AlternativeProducts'],
['shop' => 'altex', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60ca3252c0486fc28847794b&source=AlternativeProducts'],
['shop' => 'mediagalaxy', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60cb38efc0486fc2884ba1a1&source=AlternativeProducts'],
['shop' => 'flanco', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60c9ca58c0486fc288336be3&source=AlternativeProducts'],
['shop' => 'evomag', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60a8f6e0a771b2fb18843424&source=AlternativeProducts'],
['shop' => 'pcgarage', 'url' => 'https://www.pricy.ro/red/r/?shopProductId=60a8f6e0a771b2fb18843424&source=AlternativeProducts'],
];

With this PHP code:

foreach ($shops as $shop) {
$curl = curl_init($shop['url']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);

$result = curl_exec($curl);
if ($result === FALSE) {
    echo $shop['shop'] . ' url: error<br/>';
}

rewind($verbose);
$verboseLog = stream_get_contents($verbose);
preg_match_all("/redirect_to=(.*?)\'/", $verboseLog, $match);
if (isset($match[1][0])) {
    echo $shop['shop'] . ' url: ' . urldecode($match[1][0]) . '<br/>';
} else {
    echo $shop['shop'] . ' url: none<br/>';
}
}

With this i manage to get only one url output:

emag url: none
altex url: none
mediagalaxy url: none
flanco url: none
evomag url: https://www.evomag.ro/solutii-mobile-telefoane-mobile/apple-telefon-mobil-apple-iphone-12-super-retina-xdr-oled-6.1-128gb-flash-camera-duala-12-12-mp-wi-fi-5g-ios-negru-3800891.html
pcgarage url: none

I don't get any solution to get the final url if the redirect is made via javascript?

Do i have a better solution without curl, regex and stuff like this?

This is the reason why i am looking for another solution. In my case a 200 header response does not mean - final URL.

0 Answers
Related