Recently, I was working on a project that was communicating with a third-party API that, like many APIs, sends back a lot of data part of which includes a URL for the product associated with the API.
The thing about the API that was being returned was not the proper destination URL. Instead, it was a URL that ultimately redirected to another URL.
So imagine hitting, say, acme.site-info.com and having it direct to acme.com. We do this manually all the time, but I needed to get the actual – or the final – destination of the direct from the URL.
And finding the destination of a redirect with PHP is easy; however, this does assume there’s only a single redirect.
Nonetheless, here’s how to do it.