回答
在PHP中,你可以叫dns_get_record獲得與給定主機相關的DNS記錄。請注意,每個域都與零個或多個IPv4以及零個或多個IPv6地址相關聯,因此您可能想要返回一組地址。然而,這裏的
$domains = array('example.net', 'google.com', 'ipv6.google.com', 'example.404');
$ips = array_map(function($domain) {
$records = dns_get_record($domain, DNS_AAAA);
if (count($records) == 0) { // No IPv6 addresses, try IPv4
$records = dns_get_record($domain, DNS_A);
if (count($records) == 0) return false; // No mapping found
return $records[0]['ip'];
} else {
return $records[0]['ipv6'];
}
}, $domains);
var_export(array_combine($domains, $ips));
的輸出將是這樣的:
array (
'example.net' => '2001:500:88:200::10',
'google.com' => '209.85.148.105',
'ipv6.google.com' => '2a00:1450:4001:c01::93',
'example.404' => false,
)
我感謝你們所有人的幫助,但我在看代碼,如果我需要從hosts.txt文件獲取域名,代碼將如何? <?php $ hosts = Array( 'www.google.com', 'www.stackoverflow.com', 'kera.name' ); $ ips = Array();如果(($ ip = gethostbynamel($ host))!= FALSE) foreach($ hosts as $ host){ $ ips [] = $ ip; else $ ips [] = Array(); } print_r($ ips); ?> – Amenda
@Amenda對不起,我不太關注 - 代碼是Tomalaks的答案和我的無關。如果你想讓'/ etc/hosts'影響你的結果,必須手動解析它或者使用[gethostbynamel](http://php.net/manual/en/function.gethostbyname.php)來代替' dns_get_records'(它只會返回IPv4地址)。在寫作時,php還沒有'getnameinfo'函數。 – phihag
使用PHP函數gethostbyname - 獲取對應於給定的Internet主機名的IPv4地址**
<?php
$ip = gethostbyname('www.google.com');
echo $ip;
?>
你可能會看到這樣的事情:
$domains = "site1.com site2.com site3.com";
foreach(explode(" ", $domains) as $domain)
{
echo $domain ." ".gethostbyname($domain);
}
UPDATE:
如果域中有多個IP(例如:google.com),可以使用gethostbynamel - 獲取的IPv4地址的列表對應於給定的互聯網主機名。
<?php
$hosts = gethostbynamel('www.example.com');
print_r($hosts);
?>
Array
(
[0] => 74.125.225.19
[1] => 74.125.225.16
[2] => 74.125.225.18
[3] => 74.125.225.20
[4] => 74.125.225.17
)
的搜索在互聯網上不損害任何人,通過谷歌在php.net找到搜索「從URL PHP IP」一點點:
開始通過提取剛從你的網址上,然後用gethostbynamel
如果你不關心的IPv6(TSK):
<?php
$hosts = Array(
'www.google.com',
'www.stackoverflow.com',
'kera.name'
);
$ips = Array();
foreach ($hosts as $host) {
if (($ip = gethostbynamel($host)) != FALSE)
$ips[] = $ip;
else
$ips[] = Array();
}
print_r($ips);
?>
注意,有可能每個主機多個IP地址。
ideone和鍵盤都沒有設置DNS解析,因此我可以爲此提供一個實時演示。
$file = fopen("hosts.txt","r") or exit("Unable to open file");
while(!feof($file))
{
$host = fgets($file);
echo "IP: " . gethostbyname($host) . "<br />";
}
fclose($file);
您可以使用php函數gethostbyname將主機名解析爲IPv4地址。
例子:
$hosts = file("hosts.txt");
$fp = fopen("hostsip.txt", "w");
foreach ($hosts as $host) {
fwrite($fp, str_pad(trim($host), 20) . gethostbyname(trim($host)) . "\r\n");
}
etc...
或實際上是主機名的一部分的網址。 – tripleee
如果你需要的僅僅是處理文件,併產生像
array('site1.com' => 'ip1', 'site2.com' => 'ip2', 'site3.com' => 'ip3');
一個數組,那麼這段代碼應該工作(對不起,不能檢查現在,但這應該工作):
$data = file_get_contents('hosts.txt');
$lines = explode("\n", $data);
$result = array();
foreach ($lines as $line) {
$pieces = explode(' ', trim($line));
if (!empty($pieces[0]) && !empty($pieces[1])) {
$result[trim($pieces[0])] = trim($pieces[1]);
}
}
因此,在$結果你將有數組與urls作爲鍵和ips作爲 價值。
編輯:用PHP編寫的代碼
謝謝itsmee,但它會很難,如果我這樣做,因爲我有約57個網址,我想ips只是打印在分離線就像這個網站http://freelabs.info/UrlToIpOnline.aspx – Amenda
- 1. URL如何轉換爲TCP/IP請求?
- 2. 將URL列表轉換爲樹
- 3. Python - 將IP地址列表轉換爲地址範圍列表
- 4. 轉換IP範圍列表來CIDR
- 5. 將IP轉換爲反向ip查找
- 6. 將IP地址轉換爲IP範圍
- 7. 轉換長IP列表,以帶點的IP
- 8. 轉換列表和IP地址爲兩個欄,CSV
- 9. 將IP列表(帶單引號)轉換爲逗號分隔
- 10. 紅寶石:如何將IP範圍轉換爲IP的陣列
- 11. 轉換列表轉換爲字符串
- 12. SQL錶行轉換爲列表列表
- 13. 轉換列表列表變爲表
- 14. 將轉換列表轉換爲水星中的元組列表
- 15. 將轉換矩陣轉換爲列表列表
- 16. Python列表轉換爲列表字典
- 17. 將列表轉換爲列表清單
- 18. 將列表轉換爲鏈接列表
- 19. 將列表從ord轉換爲列表
- 20. 將列表轉換爲字典列表
- 21. 如何列表轉換爲Unicode列表
- 22. 轉換爲JPG,以列表的列表
- 23. 將列表轉換爲一個列表
- 24. 將列表轉換爲整數列表
- 25. 將元組列表轉換爲列表?
- 26. 將Data.Sequence轉換爲列表?
- 27. 將列表轉換爲樹
- 28. 將JSON轉換爲列表
- 29. 將列表轉換爲TreeViewItems
- 30. 將IEnumerable轉換爲列表
URL或只是域名? –