// 获取 Url 跳转后的真实地址

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

// 获取 Url 跳转后的真实地址
public static function getrealurl($url)
{
    if (empty($url))
    {
        return $url;
    }
    $header = get_headers($url, 1);
    if (empty($header[0]) || empty($header[1]))
    {
        return $url;
    }
    if (strpos($header[0], '301') || strpos($header[0], '302'))
    {
        if (empty($header['Location']))
        {
            return $url;
        }
        if (is_array($header['Location']))
        {
            return $header['Location'][count($header['Location']) - 1];
        }
        else
        {
            return $header['Location'];
        }
    }
    else
    {
        return $url;
    }
}

转载于:https://my.oschina.net/yonghan/blog/898427

你可能感兴趣的:(python,前端,c#)