PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 16th, 2008, 08:55 AM
zebe zebe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 19 zebe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 53 m 53 sec
Reputation Power: 0
CURL: Get final URL after inital URL redirects

Hi,

I need to find the redirected or 'final' URL of an initial URL which has a 302 redirect in it.

User goes to: www.1.com and gets redirected to www.2.com, I need to get 'www.2.com'

I think this can be done using CURL. Here's what I have so far, which is returning the original link...


PHP Code:
/*
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function get_web_page$url )
{
    
$options = array(
        
CURLOPT_RETURNTRANSFER => true,     // return web page
        
CURLOPT_HEADER         => false,    // don't return headers
        
CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        
CURLOPT_ENCODING       => "",       // handle all encodings
        
CURLOPT_USERAGENT      => "spider"// who am i
        
CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        
CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        
CURLOPT_TIMEOUT        => 120,      // timeout on response
        
CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    
);

    
$ch      curl_init$url );
    
curl_setopt_array$ch$options );
    
$content curl_exec$ch );
    
$err     curl_errno$ch );
    
$errmsg  curl_error$ch );
    
$header  curl_getinfo$ch );
    
curl_close$ch );

    
//$header['errno']   = $err;
   // $header['errmsg']  = $errmsg;
    //$header['content'] = $content;
    
print($header[0]);
    return 
$header;




Anyone know how I can modify this function to get the final redirected URL result?

Thanks so much for the help. I really appreciate it.

Reply With Quote
  #2  
Old July 16th, 2008, 11:17 AM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 930 tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)tagmanadvance User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 7 h 56 m 24 sec
Reputation Power: 421
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
I could be wrong, but I believe such information is returned in the header. You might want to turn 'headers' on, and 'follow redirects' off. Manually handle the redirct after extracting the new URL from the header.
__________________
"Java makes impossible things possible, but makes easy things difficult." - Somebody

Reply With Quote
  #3  
Old December 3rd, 2008, 01:48 AM
teesed teesed is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 5 teesed User rank is Corporal (100 - 500 Reputation Level)teesed User rank is Corporal (100 - 500 Reputation Level)teesed User rank is Corporal (100 - 500 Reputation Level)teesed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 51 m 54 sec
Reputation Power: 0
I've just come across this thread after a looong fruitless search.

So, here's how I did it. Took the code from zebe's post, and the suggestions from tagmanadvance. Didn't get the resolved url. However, turning the headers on and leaving the follow redirects on did the trick.

Code:
function get_web_page( $url ) 
{ 
    $options = array( 
        CURLOPT_RETURNTRANSFER => true,     // return web page 
        CURLOPT_HEADER         => true,    // return headers 
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
        CURLOPT_ENCODING       => "",       // handle all encodings 
        CURLOPT_USERAGENT      => "spider", // who am i 
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
        CURLOPT_TIMEOUT        => 120,      // timeout on response 
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 
    ); 

    $ch      = curl_init( $url ); 
    curl_setopt_array( $ch, $options ); 
    $content = curl_exec( $ch ); 
    $err     = curl_errno( $ch ); 
    $errmsg  = curl_error( $ch ); 
    $header  = curl_getinfo( $ch ); 
    curl_close( $ch ); 

    //$header['errno']   = $err; 
   // $header['errmsg']  = $errmsg; 
    //$header['content'] = $content; 
    print($header[0]); 
    return $header; 
}  
$thisurl = "http://www.example.com/redirectfrom";
$myUrlInfo = get_web_page( $thisurl ); 
echo $myUrlInfo["url"];


returns "http://www.example.com/redirectto"
Comments on this post
tagmanadvance agrees!

Reply With Quote
  #4  
Old May 29th, 2009, 06:09 AM
chandar's Avatar
chandar chandar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Chennai
Posts: 160 chandar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 39 m 50 sec
Reputation Power: 10
Send a message via MSN to chandar Send a message via Yahoo to chandar Send a message via Google Talk to chandar
Exclamation

Hi
Is it possible to get the url after i submit the authentication credentials.
Because i am getting the same login page returned when i try to give curl command.
generaly when i login from the browser to the interface if the credentials are wrong it returns to the login page with some query strings like

http://ipadaddres/loginpage.hrml?authfail=0
or
http://ipadaddres/loginpage.hrml?authfail=1
or
http://ipadaddres/loginpage.hrml?authfail=2
or
http://ipadaddres/loginpage.hrml?authfail=3

Is it possible to get this url so that i can pinpoint the validation error.


Quote:
Originally Posted by teesed
I've just come across this thread after a looong fruitless search.

So, here's how I did it. Took the code from zebe's post, and the suggestions from tagmanadvance. Didn't get the resolved url. However, turning the headers on and leaving the follow redirects on did the trick.

Code:
function get_web_page( $url ) 
{ 
    $options = array( 
        CURLOPT_RETURNTRANSFER => true,     // return web page 
        CURLOPT_HEADER         => true,    // return headers 
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
        CURLOPT_ENCODING       => "",       // handle all encodings 
        CURLOPT_USERAGENT      => "spider", // who am i 
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
        CURLOPT_TIMEOUT        => 120,      // timeout on response 
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 
    ); 

    $ch      = curl_init( $url ); 
    curl_setopt_array( $ch, $options ); 
    $content = curl_exec( $ch ); 
    $err     = curl_errno( $ch ); 
    $errmsg  = curl_error( $ch ); 
    $header  = curl_getinfo( $ch ); 
    curl_close( $ch ); 

    //$header['errno']   = $err; 
   // $header['errmsg']  = $errmsg; 
    //$header['content'] = $content; 
    print($header[0]); 
    return $header; 
}  
$thisurl = "http://www.example.com/redirectfrom";
$myUrlInfo = get_web_page( $thisurl ); 
echo $myUrlInfo["url"];


returns "http://www.example.com/redirectto"
__________________

With regards
Chandar.V.Rao
bangalore

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > CURL: Get final URL after inital URL redirects

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap