Php Curl Redirection Error

Discussion in 'PHP' started by mr_pmplx, Feb 17, 2013.

  1. #1
    this post has been resolved, thanks!
     
    Last edited: Feb 17, 2013
    mr_pmplx, Feb 17, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    why not post the answer to the question you had? so we can all learn from it?
     
    EricBruggema, Feb 17, 2013 IP
  3. ogah

    ogah Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    <?php 
    
    function get_final_url( $url, $timeout = 5 )
    {
    $url = str_replace( "&amp;", "&", 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'];
    }
    }
    
    ?>
    Code (markup):
     
    ogah, Feb 18, 2013 IP