IP address is an important part of the Internet Protocol. In the course of cURL execution, cURL will fetch a number of IP addresses, which can be retrieved in the array of curl_getinfo or curl verbose information. It’s interesting to note that these ips are different from the result retrieved by similar PHP function gethostbyname. By comparing the difference, I had further understanding on more cURL parameters and the method to get ip address in PHP.






Get IP address of a website

There 2 ways to obtain IP address using cURL.

curl_getinfo

Let’s start a cURL session.

$url = 'http://php.net/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);       
curl_exec($ch);
$info  = curl_getinfo($ch);  
curl_close($ch);  

In the array of $info, we get a lot of information

  • primary_ip, could be retrieved using the constant CURLINFO_PRIMARY_IP – IP address of the most recent connection
  • local_ip, could be retrieved using the constant CURLINFO_LOCAL_IP – Local (source) IP address of the most recent connection

To get the ip address of the website php.net

echo $info['primary_ip'];//returns 72.52.91.14

I used the following script obtained here to obtain the ip address from verbose information and get the same result.

$url = 'http://php.net/';
$wrapper = fopen('D:\temp\log.txt', 'w+');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $wrapper);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);       
$result = curl_exec($ch);
$info  = curl_getinfo($ch);
curl_close($ch);
$ip_verbose = get_curl_remote_ips($wrapper);
fclose($wrapper);

echo end($ip_verbose);  // 72.52.91.14

function get_curl_remote_ips($fp) 
{
    rewind($fp);
    $str = fread($fp, 8192);
    $regex = '/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
    if (preg_match_all($regex, $str, $matches)) {
        return array_unique($matches[0]);  
    } else {
        return false;
    }
}  

Now let’s see the result by gethostbyname,

$host = parse_url($url, PHP_URL_HOST);
$ip = gethostbyname($host );
echo $ip; //returns 72.52.91.14

I tested the result with an online ip lookup tool. It returns the same result 72.52.91.14.

I further tested the result of dns_get_record,

$dns = dns_get_record($host);
print_r($dns);

and get the consistent result.

Array ( 
	[0] => Array (
		[host] => php.net 
		[class] => IN 
		[ttl] => 202 
		[type] => A 
		[ip] => 72.52.91.14 
	) 
) 

Now I change the $url to google.com, I get primary_ip,

echo $info['primary_ip'];//returns 173.194.127.183

Get ip from verbose,

$ips = get_curl_remote_ips($wrapper);
fclose($wrapper);
echo end($ips);  //returns 173.194.127.183

Here I get different ip by using the function gethostbyname,

$host = parse_url($url, PHP_URL_HOST);
$ip = gethostbyname($host ); //173.194.127.161

After I tested the result of dns_get_record, i get very complicated DNS record for Google.com

Array ( 
  [0] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 420 
  [type] => MX 
  [pri] => 10 
  [target] => aspmx.l.google.com 
) 

  [1] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 420 
  [type] => MX 
  [pri] => 40 
  [target] => alt3.aspmx.l.google.com 
) 

[2] => Array ( 
  [host] => google.com
  [class] => IN 
  [ttl] => 420 
  [type] => MX 
  [pri] => 50 
  [target] => alt4.aspmx.l.google.com 
) 

[3] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 420 
  [type] => MX 
  [pri] => 20 
  [target] => alt1.aspmx.l.google.com 
) 
...
[5] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 56 
  [type] => AAAA 
  [ipv6] => 2404:6800:4005:805::1004 
) 

[6] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 256 
  [type] => A 
  [ip] => 173.194.127.165 
) 

[7] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 256 
  [type] => A 
  [ip] => 173.194.127.168 
) 

...

[15] => Array ( 
  [host] => google.com 
  [class] => IN 
  [ttl] => 256 
  [type] => A 
  [ip] => 173.194.127.162 
) 

[16] => Array (
  [host] => google.com 
  [class] => IN 
  [ttl] => 256 
  [type] => A 
  [ip] => 173.194.127.161 
)
) 

So the last ip in its DNS record is the same ip got by the function gethostbyname. When I tested in the online look up tool, it rendered a totally different one – 216.58.209.142. So it’s not a reliable result to get the IP address using cURL in PHP, esp. for the domains with multiple ips, or website using CND service.