确定PHP中缩短的URL的最终目标? [英] Determine Final Destination of a Shortened URL in PHP?

查看:82
本文介绍了确定PHP中缩短的URL的最终目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中做到这一点?例如

How can I do this in PHP? e.g.

bit.ly/f00b4r ==> http://www.google.com/search?q = cute +小猫

bit.ly/f00b4r ==> http://www.google.com/search?q=cute+kittens

在Java中,解决方案是这样的:

In Java, the solution is this:

您应该向发出HEAD请求 使用HttpWebRequest的网址 实例.在退货中 HttpWebResponse,检查 ResponseUri.

You should issue a HEAD request to the url using a HttpWebRequest instance. In the returned HttpWebResponse, check the ResponseUri.

只需确保AllowAutoRedirect 在HttpWebRequest上设置为true 实例(默认情况下为true). (Thx,casperOne)

Just make sure the AllowAutoRedirect is set to true on the HttpWebRequest instance (it is true by default). (Thx, casperOne)

代码是

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,弗雷德里克·莫克)

(Thx, Fredrik Mork)

但是我想用PHP做到这一点.如何? :)

But I want to do it in PHP. HOWTO? :)

推荐答案

信用进行

这篇关于确定PHP中缩短的URL的最终目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆