在php中使用get_头接收Web服务器类型
我有一个名为在php中使用get_头接收Web服务器类型,php,arrays,Php,Arrays,我有一个名为$url的数组,其中包含一些URL 我想接收每一个的Web服务器 但当我使用get_头时,我的输出将是: ( [0] => HTTP/1.0 200 OK [Date] => Mon, 24 Feb 2014 01:38:18 GMT [Expires] => -1 [Cache-Control] => private, max-age=0 [Content-Type] => text/html; charset=
$url
的数组,其中包含一些URL
我想接收每一个的Web服务器
但当我使用get_头时,我的输出将是:
(
[0] => HTTP/1.0 200 OK
[Date] => Mon, 24 Feb 2014 01:38:18 GMT
[Expires] => -1
[Cache-Control] => private, max-age=0
[Content-Type] => text/html; charset=ISO-8859-1
[Set-Cookie] => Array
(
[0] => xxx
[1] => xxx
)
[P3P] => CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
[Server] => gws
[X-XSS-Protection] => 1; mode=block
[X-Frame-Options] => SAMEORIGIN
)
Array
(
[0] => HTTP/1.1 200 OK
[Connection] => close
[Date] => Mon, 24 Feb 2014 01:40:57 GMT
[Server] => Microsoft-IIS/6.0
[X-Powered-By] => ASP.NET
[X-AspNet-Version] => 2.0.50727
[Set-Cookie] => xxx
[Cache-Control] => private
[Content-Type] => text/html; charset=utf-8
[Content-Length] => 65230
)
所以我需要将每个[Server]
放入数组中
我需要一个这样的数组
[0] => gws [1] => Microsoft-IIS/6.0
以下是我的php代码:
$urls = array("http://google.com","http://ping.com");
foreach ( $urls as $key ) {
// do something
}
我不会在foreach
循环中插入代码,因为我写错了
我不知道如何从一个数组中移动键值并将其复制到另一个数组中
$urls = array("http://google.com","http://ping.com");
$serverlist = array();
foreach ( $urls as $key ) {
$site_headers = get_headers($key,1);
if(array_key_exists("Server", $site_headers)) $serverlist[] = $site_headers["Server"];
}
var_dump($serverlist);
您可以在上看到响应(实时),只需在循环时将数组值添加到另一个数组中即可。像这样:
$urls = array("http://google.com","http://ping.com");
$servers = array();
foreach ( $urls as $key ) {
$header = get_headers($key, 1);
if(isset($header['Server'])) {
$servers[] = $header['Server'];
}
// Do stuffs...
}
在我们尝试将['Server']值推入数组之前,最好先检查它是否存在