Zac*_*urt 4 php url-routing bit.ly
我怎么能用PHP做到这一点?例如
bit.ly/f00b4r ==> http://www.google.com/search?q=cute+kittens
在Java中,解决方案是这样的:
您应该使用HttpWebRequest实例向URL发出HEAD请求.在返回的HttpWebResponse中,检查ResponseUri.
只需确保HttpWebRequest实例上的AllowAutoRedirect设置为true(默认情况下为true).(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();
}
Run Code Online (Sandbox Code Playgroud)
(Thx,Fredrik Mork)
但我想用PHP来做.如何?:)
尝试的时候,你已经找到了答案.
不过,我会用这样的东西:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://bit.ly/tqdUj");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
var_dump($url);
Run Code Online (Sandbox Code Playgroud)
一些解释:
而且,在这里,你得到:
string 'http://wordpress.org/extend/plugins/wp-pubsubhubbub/' (length=52)
Run Code Online (Sandbox Code Playgroud)
(来自我看到的包含短网址的最后一条推文之一)
这应该适用于任何缩短URL服务,独立于其特定的API.
您可能还想调整一些其他选项,例如超时; 有关更多信息,请参阅curl_setopt.
归档时间: |
|
查看次数: |
2926 次 |
最近记录: |