Skip to content

Instantly share code, notes, and snippets.

@mzdakr
Created June 12, 2014 10:26
Show Gist options
  • Save mzdakr/c83cbafd357299a88280 to your computer and use it in GitHub Desktop.
Save mzdakr/c83cbafd357299a88280 to your computer and use it in GitHub Desktop.
ホスト名のIPアドレスを解決するPHPです。CNAMEがきちんとAで解決されるまでループします。
<?php
$hostname = $_GET['hostname'];
$res = get_ipaddr($hostname);
if($res){
echo json_encode($res);
} else {
echo json_encode(array('error' => 'error'));
}
function get_ipaddr($hostname)
{
$results = dns_get_record($hostname);
foreach ($results as $result) {
if($result['type'] == 'A'){
return $result;
}
if($result['type'] == 'CNAME'){
return get_ipaddr($result['target']);
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment