开发者

Retrieve IP addresses with PHP's gethostbyname()

开发者 https://www.devze.com 2023-03-31 21:31 出处:网络
Using the code below I\'ve been able to open a .csv file with several domain names, which I then put in a PHP array and loop through. In the loop I call a function that returns the IP address of each

Using the code below I've been able to open a .csv file with several domain names, which I then put in a PHP array and loop through. In the loop I call a function that returns the IP address of each domain (gethostbyname()).

However, when I execute this, I simply get the domain names echoed back to me. When I use this function on a single domain (i.e. 开发者_开发问答not in a loop) I get the IP address as desired.

What's going wrong here? Is there some sort of in built limit or am I building the array wrong?

<?php

$urls = file('test.csv'); 

foreach($urls as $url){

    $ip = gethostbyname($url);
    echo $ip.'<br/>';
}

?>


file() returns the contents of the file as an array, but also includes the linebreak characters at the end of the lines, so most likely you're trying to do a lookup on something like google.com\n. Try using

$urls = file('test.csv', FILE_IGNORE_NEW_LINES);

or

foreach($urls as $url) {
   $ip = gethostbyname(trim($url)); // note the use of trim()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消