'How do I check all the redirects of a URL and get it as an array?

I'm making a site which checks all the URL redirects, just like wheregoes.com. This site doesn't provide any APIs.

I have to get all redirects, then select the 2nd and 3rd redirect and perform some actions with it. If I use curl I only get the last redirected URL.

I want to get all the URLs redirected like

Page 1 -> Page 2 -> Page 3 -> Page 4 

I want to see the URL of Page 2 and Page 3 then. How can it be done using curl with php or any other way?

The solutions I have found so far are only showing the last URL.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source