cURL / Mailing Lists / curl-users / Single Mail

curl-users

Fetching only the destination URLs

From: Phoenix <phoenix.kiula_at_gmail.com>
Date: Wed, 11 Aug 2010 09:36:12 +0800

Hi. Loving CURL but just getting used to the truckload of features :)

A section on my website has a feature where people can submit some
info, part of which is their URL.

I'm now discovering that some cretins give us URLs that are actually
forwarded to other URLs which are in turn forwarded to other URLs.

So I'm trying to write a script that tells me the *final* destination URL.

I think I can do this with CURL, right? My humble code for now:

$url = 'http://example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, true);
$url = curl_exec($ch);
curl_close($ch);
echo $url;

All I need is the final URL. Don't need any content etc. I just want
to "FOLLOWLOCATION" as many times as needed, and just report the
ultimate destination URL. But the above code prints out the html code
of the final URL.

What should I do so I only get the final URL -- and in the fastest way
possible, because I'm not interested in the content at all.

Thanks for any advice!
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-08-11