【问题标题】:Get Final Redirect URL on Tricky Redirects获取棘手重定向的最终重定向 URL
【发布时间】:2014-12-07 17:03:16
【问题描述】:

您好,我正在尝试在 PHP 中遵循 HTTP 重定向后获取最终 URL,我使用了 this solution :

但对于某些网站,此功能不起作用,例如 http://tassels.com.hk --- 将是 --> http://www.tassels.com.hk/en/index.php

/**
 * get_redirect_url()
 * Gets the address that the provided URL redirects to,
 * or FALSE if there's no redirect. 
 *
 * @param string $url
 * @return string
 */
function get_redirect_url($url){
    $redirect_url = null; 

    $url_parts = @parse_url($url);
    if (!$url_parts) return false;
    if (!isset($url_parts['host'])) return false; //can't process relative URLs
    if (!isset($url_parts['path'])) $url_parts['path'] = '/';

    $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
    if (!$sock) return false;

    $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n"; 
    $request .= 'Host: ' . $url_parts['host'] . "\r\n"; 
    $request .= "Connection: Close\r\n\r\n"; 
    fwrite($sock, $request);
    $response = '';
    while(!feof($sock)) $response .= fread($sock, 8192);
    fclose($sock);

    if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
        if ( substr($matches[1], 0, 1) == "/" )
            return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
        else
            return trim($matches[1]);

    } else {
        return false;
    }

}

/**
 * get_all_redirects()
 * Follows and collects all redirects, in order, for the given URL. 
 *
 * @param string $url
 * @return array
 */
function get_all_redirects($url){
    $redirects = array();
    while ($newurl = get_redirect_url($url)){
        if (in_array($newurl, $redirects)){
            break;
        }
        $redirects[] = $newurl;
        $url = $newurl;
    }
    return $redirects;
}

/**
 * get_final_url()
 * Gets the address that the URL ultimately leads to. 
 * Returns $url itself if it isn't a redirect.
 *
 * @param string $url
 * @return string
 */
function get_final_url($url){
    $redirects = get_all_redirects($url);
    if (count($redirects)>0){
        return array_pop($redirects);
    } else {
        return $url;
    }
}

echo get_final_url("http://tassels.com.hk");

【问题讨论】:

  • 我试过很多功能 curl ....ect,但总是不适用于某些网站
  • 重定向是通过 JavaScript:window.location = "http://www.tassels.com.hk/en/index.php"。 cURL 永远不会找到那个。
  • @cbuckley 是否有任何库可以处理所有这些类型的重定向?
  • 你需要一些可以执行 JavaScript 的东西;你看过PhantomJS这样的东西吗?

标签: php http url host url-redirection


【解决方案1】:

我发现我的函数不遵循 javascript 和元标记重定向,您可以使用正则表达式读取 html 代码并找到 javascript/jQuery 或元标记重定向

获取 url 表单 javascript 和元标记重定向的代码:

if (preg_match('/window\.location\.replace\([\s]{0,}[\"\'](.*)[\"\'][\s]{0,}\)/i', $response, $redirect_result['1']) ||
      preg_match('/\$\(location\)\.attr\([\s]{0,}[\"\'][\s]{0,}href[\s]{0,}[\"\'][\s]{0,}\,[\s]{0,}[\"\'][\s]{0,}(.*)[\s]{0,}[\"\'][\s]{0,}\)/i', $response, $redirect_result['2']) ||
      preg_match('/window\.location[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['3']) ||
      preg_match('/window\.location\.href[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['4']) ||
      preg_match('/window\.href[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['5']) ||
      preg_match('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $response, $redirect_result['6'])) {
    for ($i = 1; $i <= 6; $i++) {
      if ($redirect_result[$i]) {
        $window_location_final = $redirect_result[$i];
        break;
      }
    }
    $window_location_final = end($window_location_final);
    if (substr($window_location_final, 0, 1) === '/' && valid_url(trim($redirect_url))) {
      $window_location_final = rtrim($redirect_url, '/') . $window_location_final;
    }
    $window_location_final = valid_url(trim($window_location_final), TRUE) ? trim($window_location_final) : '';
    if ($window_location_final) {
      $redirect_url = $window_location_final;
    }    
    return $redirect_url;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2021-09-15
    • 2014-02-22
    • 2013-02-03
    相关资源
    最近更新 更多