【问题标题】:PHP Getting A Records Only Using dns_recordPHP 仅使用 dns_record 获取记录
【发布时间】:2017-10-20 19:20:12
【问题描述】:

我正在为服务器制作一个 IP 抓取脚本,但我的代码存在一些问题。我想只使用 dns_get_record 获取服务器的 IP 地址,而不会在默认输出中出现额外的膨胀。但是,我当前的脚本在运行时显示空白输出。和不。我不能简单地使用 gethostbyname,因为它必须与查询运行代码的服务器的 wan ips 兼容。到目前为止,这是我的代码:

<?php
function test() {
    $host = "google.com";
     $result = dns_get_record("$host", DNS_A);
foreach ($result as $record) {
    echo $record['target'];
    }
}
?>

【问题讨论】:

  • 你试过print_r($result);吗?
  • 从哪里调用 test()?仅仅因为该函数是在脚本中定义的,它仍然需要被调用。它不会仅根据定义自动运行。在结束 PHP 标记之前,添加:test();
  • 稍后在表格中调用该函数。我尝试了你的建议,但它只显示这个(我只想要 ip): Array ( [host] => google.com [class] => IN [ttl] => 135 [type] => A [ip] => 172.217.4.142 ) 数组 ( [host] => google.com [class] => IN [ttl] => 135 [type] => A [ip] => 172.217.4.142 ) 数组 ( [host] => google.com [class] => IN [ttl] => 135 [type] => A [ip] => 172.217.4.142)
  • 您在某处看到 IPv4 地址吗? ......... 提示:试试$record['ip'] ;)
  • 是的。这种改变奏效了!感谢您的帮助!

标签: php dns ip gethostbyname dns-get-record


【解决方案1】:

固定代码(正常工作):

function test() {
    $host = "google.com";
     $result = dns_get_record("$host", DNS_A);
foreach ($result as $record) {
    echo $record['ip'];
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 2022-12-19
    • 2020-08-19
    • 1970-01-01
    • 2011-12-11
    • 2021-02-15
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多