PHP Curl跟随重定向

10

我试图在学习过程中提高页面抓取技能,想采用一些策略来达到目的。

其中一个问题是,某些网站将使用内部链接,然后重定向到外部链接,而我尚未能够解决这个问题。

我想修改一些curl代码,跟随重定向直到它们停止,然后获取最终停留位置的URL地址。

有人可以为我推荐一些代码吗?

我现在有这段代码,但它无法正确地遵循重定向。

        $opts = array(CURLOPT_URL => $url,
                      CURLOPT_RETURNTRANSFER => true,
                      CURLOPT_HEADER => true,
                      CURLOPT_FOLLOWLOCATION => true);      

        $curl = curl_init(); 
        curl_setopt_array($curl, $opts);  
        $str = curl_exec($curl);  
        curl_close($curl);  

什么是指向外部链接的内部链接?如果 followlocation 开启,Curl 应该使用 30* 头进行重定向。 - Explosion Pills
2个回答

25
http://php.net/manual/en/ref.curl.php
   function get_final_url( $url, $timeout = 5 )
 {
    $url = str_replace( "&", "&", urldecode(trim($url)) );

   $cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );

if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
    ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
    $headers = get_headers($response['url']);

    $location = "";
    foreach( $headers as $value )
    {
        if ( substr( strtolower($value), 0, 9 ) == "location:" )
            return get_final_url( trim( substr( $value, 9, strlen($value) ) ) );
    }
}

if (    preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
        preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
)
{
    return get_final_url ( $value[1] );
}
else
{
    return $response['url'];
   }
}

我刚刚使用了这个,它完美地运行了!非常感谢与我分享。 - David
@manish 有没有什么更简单的方法可以使用 curl_getinfo($curl, CURLINFO_REDIRECT_URL) - Svish

1
如果您无法使用,建议您使用像这样的递归方法:
function getUrl($url, $count) {

    // max number of redirects
    if ($count > 5) {
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if (!$data) {
        return false;
    }

    $dataArray = explode("\r\n\r\n", $data, 2);

    if (count($dataArray) != 2) {
        return false;
    }

    list($header, $body) = $dataArray;
    if ($httpCode == 301 || $httpCode == 302) {
        $matches = array();
        preg_match('/Location:(.*?)\n/', $header, $matches);

        if (isset($matches[1])) {
            return getUrl(trim($matches[1]), $count + 1);
        }
    } else {
        return $body;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接