What I'd like to do is find out what is the last/final URL after following the redirections.
I would prefer not to use cURL. I would like to stick with pure PHP (stream wrappers).
Right now I have a URL (let's say http://domain.test), and I use get_headers() to get specific headers from that page. get_headers will also return multiple Location:
headers (see Edit below). Is there a way to use those headers to build the final URL? or is there a PHP function that would automatically do this?
Edit: get_headers() follows redirections and returns all the headers for each response/redirections, so I have all the Location:
headers.
And, as always, give credit:
http://w-shadow.com/blog/2008/07/05/how-to-get-redirect-url-in-php/
xaav answer is very good; except for the following two issues:
Some sites will not work since they will not recognise the underlying user agent (client browser) => This is simply fixed by adding a User-agent header field: I added an Android user agent (you can find here http://www.useragentstring.com/pages/useragentstring.php other user agent examples according you your need):
$request .= "User-Agent: Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30\r\n";
Here's the modified answer:
Additionally...
As was mentioned in a comment, the final item in
$headers['Location']
will be your final URL after all redirects. It's important to note, though, that it won't always be an array. Sometimes it's just a run-of-the-mill, non-array variable. In this case, trying to access the last array element will most likely return a single character. Not ideal.If you are only interested in the final URL, after all the redirects, I would suggest changing
to
... which is just if short-hand for
This fix will take care of either case (array, non-array), and remove the need to weed-out the final URL after calling the function.
In the case where there are no redirects, the function will return
false
. Similarly, the function will also returnfalse
for invalid URLs (invalid for any reason). Therefor, it is important to check the URL for validity before running this function, or else incorporate the redirect check somewhere into your validation.While the OP wanted to avoid
cURL
, it's best to use it when it's available. Here's a solution which has the following advantageslocation
header name (both xaav and webjay's answers do not handle this)Here's the function:
Here's a more verbose version which allows you to inspect the redirection chain rather than let curl follow it.
As an example of redirection chain which this function handles, but the others do not, try this:
At the time of writing, this involves 4 requests, with a mixture of
Location
andlocation
headers involved.