Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将Url列表转换为ip?_Php_Javascript - Fatal编程技术网

Php 将Url列表转换为ip?

Php 将Url列表转换为ip?,php,javascript,Php,Javascript,有没有把URL列表转换成ip列表的程序? 例如: php具有dns查找功能。互联网上也有很多类似的服务 在php中,您可以调用以获取与给定主机名关联的记录。请注意,每个域都与零个或多个IPv4以及零个或多个IPv6地址关联,因此您可能希望返回一个地址数组。然而,这里有一个 $domains = array('example.net', 'google.com', 'ipv6.google.com', 'example.404'); $ips = array_map(function($domai

有没有把URL列表转换成ip列表的程序? 例如:


php具有dns查找功能。互联网上也有很多类似的服务

在php中,您可以调用以获取与给定主机名关联的记录。请注意,每个域都与零个或多个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,
)
使用php函数-获取与给定Internet主机名对应的IPv4地址**

<?php
$ip = gethostbyname('www.google.com');

echo $ip;
?>
更新:

如果域有多个ip(例如:google.com),您可以使用-获取与给定Internet主机名对应的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
)

排列
(
[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上通过谷歌搜索“ip from url php”,在互联网上搜索一点不会伤害任何人:


首先从URL中提取主机,然后在不关心IPv6(tsk)的情况下使用:


请注意,每个主机可能有多个IP

ideone和codepad似乎都没有设置DNS解析,因此我可以为此提供实时演示。

$file=fopen(“hosts.txt”、“r”)或exit(“无法打开文件”);
$file = fopen("hosts.txt","r") or exit("Unable to open file");

while(!feof($file))
{
    $host = fgets($file);
    echo "IP: " . gethostbyname($host) . "<br />";
}

fclose($file);
而(!feof($file)) { $host=fgets($file); echo“IP:.gethostbyname($host)。”
; } fclose($文件);
您可以使用php函数将主机名解析为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...

如果您只需要处理一个文件并生成一个数组,如

array('site1.com' => 'ip1', 'site2.com' => 'ip2', 'site3.com' => 'ip3');
那么这段代码应该可以工作(很抱歉,现在无法检查,但应该可以工作):

因此,在$result中,您将使用URL作为键、IP作为值的数组


编辑:用PHP编写的代码

非常感谢大家的帮助,但我正在查看代码,如果我需要从hosts.txt文件中获取域名,代码将如何处理@阿蒙达,对不起,我不太明白-那个代码是托马拉克的答案,和我的无关。如果您想让
/etc/hosts
影响您的结果,必须手动解析它,或者使用而不是
dns\u get\u记录(只返回IPv4地址)。在撰写本文时,php还没有一个
getnameinfo
函数。我的意思是,如果我可以从名为hosts.txt的文件中获取主机,然后仅以行形式打印ip?谢谢abbas,但当我测试时,它会打印除hosts.txt的最后一个url之外的所有url(域名),这是一个ipThen在调用函数gethostbyname()之前检查记录。如果记录是主机名,请使用该函数,否则只需打印该记录,因为它已经是IP。:^\谢谢阿巴斯,如果你把我的问题解决了的帖子贴出来,那就太好了,因为我不明白你的意思!IP:xxxx.com IP:xxxx.org IP:xxxx.com IP:site.com IP:site1.com IP:63.247.140.166您不明白的是什么?只要确保你有一个文本文件“hosts.text”。第一行代码打开文件,while循环读取文件中的每一行并调用函数。但正如其他人所说:一台主机可能有零个或多个ip地址,因此最好返回一个包含零个、一个或多个主机地址的数组。检查本主题中的其他反应,了解一些代码。让我指定我的问题,请您不理解我,也许我确定我已将网站放入hosts.txt中,但当我尝试运行您给我的代码时,它只是为文本中的最后一个网站获取一个ip,我不希望获取hosts.txt中URL的所有ip示例“ip:flyertalk.com IP:zagg.com IP:adventuregamers.com IP:kidsdinos.com IP:174.46.134.54’或者实际上是URL的主机名部分。谢谢,但是如果我这样做会很困难,因为我有大约57个URL,我希望IP像这个网站一样以分隔线打印出来
$file = fopen("hosts.txt","r") or exit("Unable to open file");

while(!feof($file))
{
    $host = fgets($file);
    echo "IP: " . gethostbyname($host) . "<br />";
}

fclose($file);
$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...
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]);
    }
}