---
title: "PHP 批量根据URL获取IP"
author: "onestopweb"
date: "2017-03-08T18:09:54Z"
tags: []
categories: ["编程语言"]
slug: "php-url-ip"
draft: false
lastmod: "2017-03-08T18:09:54Z"
id: 2360989
url: "https://onestopweb.iteye.com/blog/2360989"
---

```
<meta charset="utf-8">
<?php
$urls = array('http://onestopweb.iteye.com/category','http://onestopweb.cn/','http://www.iteye.com/');
$arr = array();
for($i=0;$i<count($urls);$i++){
	//一条完整的链接由协议、主机、端口、路径四个部分组成
	//获取它的主机部分，例如：onestopweb.iteye.com
	$url = parse_url($urls[$i]);
	//gethostbyname 获取对应IP
	$arr[$url[host]] = gethostbyname($url[host]);
}
/**
 * sort() - 以升序对数组排序
 * rsort() - 以降序对数组排序
 * asort() - 根据值，以升序对关联数组进行排序
 * ksort() - 根据键，以升序对关联数组进行排序
 * arsort() - 根据值，以降序对关联数组进行排序
 * krsort() - 根据键，以降序对关联数组进行排序
 * */
asort($arr);
foreach($arr as $k=>$v){
	echo $k.' -> '.$v.'<br>';
}
?>
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0123/5662/a411c93c-8cdc-3fc1-a462-61af82d51881.png)