![](/img/trans.png)
[英]How can I resolve a domain name to an IP address when the PHP functions fail?
[英]Is it possible to resolve domain names to an IP address with PHP?
好,非常简单的问题。 我想这对您来说是个好消息。
更详尽的解释:
我有一个PHP脚本,可以将URL添加到数据库中。 我希望脚本也将URL解析为IP地址,也将其存储。 但是,某些URL如下所示:
http://111.111.111.111/~example/index.php
因此,它也需要与此配合。
我不知道这是可能的,但它才有意义这将是。
因此:可能吗?如果可以,怎么做?
echo gethostbyname('stackoverflow.com'); // 69.59.196.211
您可以使用parse_url从网址中获取主机:
$urlParts = parse_url('http://111.111.111.111/~example/index.php');
print_r($urlParts);
/*
Array
(
[scheme] => http
[host] => 111.111.111.111
[path] => /~example/index.php
)
*/
gethostbynamel() -获取与给定Internet主机名相对应的IPv4地址列表 。
用法:
$ips = gethostbynamel('stackoverflow.com');
/*
Array
(
[0] => 69.59.196.211
)
*/
echo '<pre>';
print_r($ips);
echo '</pre>';
$ips = gethostbynamel('google.com');
/*
Array
(
[0] => 74.125.39.105
[1] => 74.125.39.106
[2] => 74.125.39.147
[3] => 74.125.39.99
[4] => 74.125.39.103
[5] => 74.125.39.104
)
*/
echo '<pre>';
print_r($ips);
echo '</pre>';
要使主机脱离URL,您可以使用以下一种方法:
// 111.111.111.111
echo parse_url('http://111.111.111.111/~example/index.php', PHP_URL_HOST);
是的, gethostbyname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.