php获取重定向的URL

时间:2017-06-20 18:15:26

标签: php curl url-redirection

function get_redirect_final_target($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
    curl_exec($ch);
    $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    if ($target)
        return $target;
    return false;
}

我原来的链接

$url = "http://www.mylink";

echo重定向链接,但它会回显相同的链接,因为它无法检测到任何重定向标头。但我的链接将有302重定向。

echo get_redirect_final_target($url);

使用其他正常的重定向链接将起作用,但我的链接应该重定向,但它无法获取标题位置链接。 任何帮助都非常有用!

1 个答案:

答案 0 :(得分:0)

只需使用get_headers PHP函数,看看你是否得到200或302。

相关问题