zebe Posted July 15, 2008 Share Posted July 15, 2008 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... /* * 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.