How can I do this in PHP? e.g.
bit.ly/f00b4r ==> http://www.google.com/search?q=cute+kittens
In Java, the solution is this:
You should issue a HEAD request to the url using a HttpWebRequest instance. In the returned HttpWebResponse, check the ResponseUri.
Just make sure the AllowAutoRedirect is set to true on the HttpWebRequest instance (it is true by default). (Thx, casperOne)
And the code is
private static string GetRealUrl(string url)
{
WebRequest request = WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Head;
WebResponse response = request.GetResponse();
return response.ResponseUri.ToString();
}
(Thx, Fredrik Mork)
But I want to do it in PHP. HOWTO? :)
Here is my solution. I coded it, because none of the above worked correctly.
CREDIT GOES TO http://forums.devshed.com/php-development-5/curl-get-final-url-after-inital-url-redirects-544144.html
Did you read the bit.ly API? specifically here ?
I can't see the issue. Are you talking about possible redirects ?
The time to try, you already found the answer.
Still, I would have gone with something like this :
Some explanations :
And, here, you get :
(Comes from one of the last tweets I saw that contained a short URL)
This should work with any shortening-URL service, independantly of their specific API.
You might also want to tweak some other options, like timeouts ; see curl_setopt for more informations.