0

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!

Madalin
  • 21
  • 3
  • You can get some additional time data with curl_getinfo for example CURLINFO_NAMELOOKUP_TIME, CURLINFO_CONNECT_TIME and CURLINFO_REDIRECT_TIME. In your example there are two separate DNS lookups, 3 different connections (two of those with TLS handshakes) so the response time seems more or less accurate. – Dobromir Velev Dec 17 '20 at 19:41
  • As far as i understand this can help me just to get some logs for the timing / loading. Do you have some tips about doing this faster? – Madalin Dec 18 '20 at 21:37
  • Until you know where the delays are it will be hard to tell what can be optimized. A DNS cache might be helpful, disabling TLS certificate verification will probably shave off a few milliseconds, running curl directly will avoid PHP overheads. But if those delays are network related your best course of action is to get a hosting closer to the domains you are testing – Dobromir Velev Dec 20 '20 at 04:30

1 Answers1

0

I personally think you're doing fine and curl is a great way to go. Anyway I guess you won't have an option here. Since not the client but the server defines the effective URL you cannot know it faster than trying to call it, wait for all redirects and if any delivers status OK use this URL. For every redirect the server is asked for response and it responds with a redirection, this can lead to high waiting times.

Lukas Fink
  • 627
  • 6
  • 18
  • Ok, i understand that but i cannot explain to myself such a loading time. As far as i research, `CURLOPT_NOBODY, true` should "inspect" just the header. But either with `CURLOPT_NOBODY` set to `true` or `false` in no speed improvment. :( – Madalin Dec 16 '20 at 22:09
  • You won't get any results just inspecting the header. Some curl options overwrite others, I can't say which exactly but you can look it up. But it doesn't change the behaviour. Only with the final answer you know the final URL. And the loading times are indeed high, but have you checked how fast it responds using the effective URL? Is it much faster? And it seems like there is some URL shorting between, this might take a while looking up and redirecting, so to me it's not a big surprise – Lukas Fink Dec 16 '20 at 22:12
  • Indeed i can't control the load time of the links but they are part of a major affiliate marketing network from my country (i kinda trust them they have a optimised network). Do you have some directions about the overwritten options you have mentioned? – Madalin Dec 16 '20 at 22:23