views:

127

answers:

3

I'm using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually redirected?

A: 

If you do not need the final body you can do it this way:

Set CURLOPT_HEADER and CURLOPT_NOBODY. The header "Location" should be returned and will contain the new url. Then perform the request with the new url if necessary.

Kevin Peno
+3  A: 

You can do something like:

curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // returns the last effective URL
Alix Axel
Nice. Didn't know about this one. Considering the number of curl options, its not always easy to find them. Thanks.
Kevin Peno
+1  A: 
$ch = curl_init( "http://websitethatredirects.com" );
$curlParams = array(
   CURLOPT_FOLLOWLOCATION => true,
);
curl_setopt_array( $ch, $curlParams );
$ret = curl_exec( $ch );
$info = curl_getinfo( $ch );
print $info['url'];

This will show you the URL that you were ultimately redirected to.

Ian Van Ness