What i want to achieve is to get the final URL based on a first URL (some hardcoded affiliate hyperlinks).
Let's say that the redirect chain can look something like this (basically there are max 3 redirects):
http://example.com/jhishsuisasd ->
https://example.com/go/vendor/zxcvbn ->
https://www.vendorname.com/tvs/smarttv-lg-oled-nj4432/343/
After some research i made up with the following code based on php curl:
function last_url($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
$html = curl_exec($ch);
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $redirectedUrl;
}
I have tested the code like this:
$starttime = microtime(true); // Top of page
echo last_url('http://example.com/jhishsuisasd');
$endtime = microtime(true); // Bottom of page
printf("<!-- Page loaded in %f seconds -->", $endtime - $starttime );
The thing is the code works (it returns me the final URL) but loads very very slow.
The code return https://www.vendorname.com/tvs/smarttv-lg-oled-nj4432/343/
The issue is the loading time. The test from above returns almost half a second loading time:
<!-- Page loaded in 0.451242 seconds --> - first load
<!-- Page loaded in 0.497280 seconds --> - second reload
<!-- Page loaded in 0.555479 seconds --> - third reload
<!-- Page loaded in 0.484253 seconds --> - 4th reload
Please note that i am a beginner with PHP and i know that there is a possibility of doing something wrong from the beginning.
Is this the situation? It's something wrong with the code?
Is there a better alternative to get the final URL? Maybe curl is to much? Maybe some premade class?
Thank you in advance!