如何从PHP中的域名获取IP?

时间:2016-08-08 14:33:37

标签: php ip ping

我在DOS中执行我的代码)这是我的代码:

<?php
$handle = fopen("domann.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $ip = gethostbyname($line);
        echo "reading: ".$line. " ip: ".$ip  ;
    }

    fclose($handle);
} else {
    // error opening the file.
}

?>

我想要这样的事情:

阅读:www.abc.it ip:211.195.239.122

问题不在gethostbyname()函数中! 它必须是fgets($ handle)中的某些内容,每行返回一个\ n

此解决方案:

$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {

      $line=str_replace("\r\n","",$line);
        $ip = gethostbyname($line);

      echo "reading: ".$line." ip: ".$ip;
      echo "\n";
    }

    fclose($handle);
} else {
    echo "connection error";
}

3 个答案:

答案 0 :(得分:1)

嘿那里@Luca我已经找到了为什么你的代码我正在工作,因为在你的domann.txt中你可能已经将网站命名为 http://google.com 或者 http://www.google.com 哪个无法正常工作,唯一适用于 www.google.com 的方式,因此在您的domann.txt名称中,您的网站会像www一样。后缀,没有更多,这应该解决它。证明Works Fine

答案 1 :(得分:0)

试试这个,

http://php.net/gethostbyname

$names = file('domann.txt');
 foreach ($names as $name) {
     $ip = gethostbyname($name);
     echo  $ip.' <br>';
 }

答案 2 :(得分:0)

此解决方案:

$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {

      $line=str_replace("\r\n","",$line);
        $ip = gethostbyname($line);

      echo "reading: ".$line." ip: ".$ip;
      echo "\n";
    }

    fclose($handle);
} else {
    echo "connection error";
}