The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
CURL: Get final URL after inital URL redirects
Discuss CURL: Get final URL after inital URL redirects in the PHP Development forum on Dev Shed. CURL: Get final URL after inital URL redirects PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 16th, 2008, 08:55 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 19
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.
|

July 16th, 2008, 11:17 AM
|
 |
Kage Bunshin
|
|
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
|
|
|
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
|

December 3rd, 2008, 01:48 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 5
  
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"
|

May 29th, 2009, 06:09 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: Chennai
Posts: 160
Time spent in forums: 16 h 39 m 50 sec
Reputation Power: 10
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|