最直接可靠的方式是用 curl 发起 HEAD 或 GET 请求并禁用自动跳转,手动跟踪 Location 响应头;PHP 中 get_headers() 有局限性,推荐用 file_get_contents() + stream_get_meta_data() 逐层解析跳转链。
curl 检查短链接跳转是否成功PHP 本身不内置“还原短链”功能,实际是模拟 HTTP 请求,观察响应状态码和重定向目标。最直接可靠的方式是用 curl 发起 HEAD 或 GET 请求,禁用自动跳转,手动跟踪 Location 响应头。
-I(HEAD)或 -L -s -o /dev/null -w "%{http_code}\n%{redirect_url}\n" 来获取最终状态与跳转链curl 自动跳转(-L),会丢失中间跳转信息;验证“还原”过程时建议先关掉自动跳转User-Agent 或 Referer,空 UA 可能被拦截返回 403 或跳转到错误页get_headers() 获取跳转地址的局限性get_headers() 默认不跟随重定向,但返回结果依赖服务器是否在响应头中明确写入 Location,且无法处理多级跳转(比如 A→B→C)或 JS 跳转(非 HTTP 重定向)。
$context = stream_context_create(['http' => ['max_redirects' => 0]]),否则 PHP 7.4+ 可能默认跟随Location: (.+),不能只取 [1] —— 多数情况下 [1] 是第二个响应(即跳转后),不是原始 Location
get_headers() 会直接失败,不抛错也不返回,需配合 error_get_last() 检查file_get_contents() + stream_get_meta_data() 安全还原跳转链这是兼顾可控性与兼容性的做法:手动控制跳转次数,逐层提取 Location,避免无限重定向,也便于记录每一步状态。
function resolveShortUrl($url, $maxRedirects = 3) {
$redirects = [];
$current = $url;
for ($i = 0; $i < $maxRedirects; $i++) {
$ctx = stream_context_create([
'http' => [
'met
hod' => 'HEAD',
'max_redirects' => 0,
'ignore_errors' => true,
'timeout' => 5,
'header' => "User-Agent: Mozilla/5.0 (X11; Linux x86_64)\r\n"
]
]);
$content = @file_get_contents($current, false, $ctx);
$meta = stream_get_meta_data($http_response_header ?? []);
$headers = $http_response_header ?? [];
$location = null;
foreach ($headers as $h) {
if (stripos($h, 'Location:') === 0) {
$location = trim(substr($h, 9));
break;
}
}
if (!$location) break;
$redirects[] = ['from' => $current, 'to' => $location, 'status' => $meta['wrapper_type'] === 'http' ? $meta['http_code'] : 0];
$current = $location;
}
return $redirects;
}
HEAD 方法,避免下载大文件拖慢验证速度;但某些服务(如 bit.ly)对 HEAD 返回 405,此时需 fallback 到 GET + stream_set_timeout()
$http_response_header 的作用域问题:它只在 file_get_contents() 当前作用域有效,不能靠全局变量捕获这类平台短链不是纯 HTTP 重定向,而是服务端根据 User-Agent、IP、访问频次动态返回不同结果,甚至插入中间落地页(如“点击继续”按钮页)。
weixin.qq.com)在无微信 UA 时大概率返回 404 或跳转到公众号主页,curl -A "MicroMessenger" 才可能拿到真实目标t.cn)需带 Referer: https://weibo.com/,否则可能返回风控页 HTML,而非 Location 头Location,基本可判定是前端 JS 跳转,PHP 后端无法还原text/html 或 application/pdf)。中间任意一跳超时、返回 4xx/5xx、或 Content-Type 异常,都意味着该短链当前不可用。