参考自一缸水的C#还原短地址,原理是模拟请求短地址再获取header中的Location内容,就能拿到正确的页面url。
PHP代码:
function getRealUrl($shortUrl) { $header = get_headers($shortUrl, 1); if ($header && $header['Location']) { $location = $header['Location']; if (is_array($location)) { return $location[0]; } else { return $location; } } return $shortUrl; } // For http://t.co/xxx if (isset($_REQUEST['tco'])) { $real_tco = getRealUrl('http://t.co/' . $_REQUEST['tco']); } // For full short URL if (isset($_REQUEST['shorturl'])) { $real_url = getRealUrl($_REQUEST['shorturl']); }
参考源程序:realurl.php,测试
相关日志
Tags: 技巧