I don't think there is a way to get this IP address directly from curl.
But something like this might do the trick:
First make a curl_getinfo request and use curl_getinfo to get the βrealβ URL that was extracted - this is because the first URL can be redirected to another, and you want the final version:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $content = curl_exec($ch); $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close($ch); var_dump($real_url); // http://www.google.fr/
Then use parse_url to extract part of the host from this final url:
$host = parse_url($real_url, PHP_URL_HOST); var_dump($host); // www.google.fr
And finally, use gethostbyname to get the IP address corresponding to this host:
$ip = gethostbyname($host); var_dump($ip); // 209.85.227.99
Well...
This solution ^^ It should work in most cases, I suppose, although I'm not sure that you will always get the βrightβ result if there is some kind of load balancing mechanism ...
Pascal martin
source share