When I issue an old URL :
http://maps.google.com/maps?ll=50.93554167,4.36341667&t=k&z=19
Google Maps returns (redirects to) a new URL :
https://www.google.com/maps/@50.9355417,4.3634167,169m/data=!3m1!1e3?entry=ttu&g_ep=EgoyMDI0MTIwMy4wIKXMDSoASAFQAw%3D%3D
I have a large list of old Google Maps URLs that I want to convert to the new format.
Before I can do that I need to harvest all these redirected URLs to determine the approximate relatonship between the old zoomlevels and some equivalent (rounded) altitude value in the new format.
I will use PHP to accomplish this.
The scripts suggested in php get url of redirect from source url do not work, they return a URL that is similar to the source URL.
How to do this ?
2
To get the new URL, you would traditionally make a request and parse the response to look for the Location
header. However, the curl functions give us a super-handy method so we don’t need to do that ourselves. Just make the request and then call curl_getinfo()
:
$url = 'http://maps.google.com/maps?ll=50.93554167,4.36341667&t=k&z=19';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
print_r(curl_getinfo($curl));
curl_close($curl);
This gives us all sorts of useful data, including:
...
[http_code] => 302
[redirect_url] => https://www.google.com:443/maps?ll=50.93554167,4.36341667&t=k&z=19
...
You can also restrict the info packet to just that one value that you want. Now just wrap it in a nice function, and you can call it per URL:
function getRedirectUrl(string $url): ?string
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
$redirectUrl = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
curl_close($curl);
return $redirectUrl ?: null;
}
1