Get redirected URL from URL [duplicate]

拜拜、爱过 提交于 2019-12-13 12:35:20

问题


The title is quite cryptic but essentially whats happening is I'm calling the Google Places Photo API with the following url

https://maps.googleapis.com/maps/api/place/photo?key=123456789&photoreference=CnRvAAAAV_35nLjUhcQXkoVdwN2YGP58Z3trNMDCRXznXqR2_0FbxXv60ssT2JN0ryI_ZP9-371u3JycqlH5oxLy8-bo2IkbkAfWD7fjyiytcEtWmk76Cseyxj9OlDozspXy8Cu7NFphl4N1AXSnj6QadSy6fxIQvc6Kiai-6HxMphQ7WCmYpBoUwxVnwEHl5zo0EbuoWCObJYTmAuA&maxwidth=400&maxheight=400&sensor=false

(I've changed my API key)

When accessing this link via the browser it redirect to a different url:

https://lh3.googleusercontent.com/-WjnhIv4ry4I/T6Wg4kRb4kI/AAAAAAAuqqc/t6yVdN-jcC0/s1600-w220-h151/City%2BRestaurant

I need a way to get that second URL using PHP. Using cURL gives me a 302 "document has been moved to here" message.

Anyone know how I can get that second url via PHP?


回答1:


Make your request using curl with curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); which will cause curl to follow the redirect.

Then, you can use curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); which will give you the last effective URL that curl fetched.

Alternatively, you can set CURLOPT_FOLLOWLOCATION to false, and set CURLOPT_HEADERS to true, and parse the redirect location out yourself using a simple regex.



来源:https://stackoverflow.com/questions/15297000/get-redirected-url-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!