在php中使用get_headers接收Web服务器类型

时间:2014-02-24 01:51:29

标签: php arrays

我有一个名为$urls的数组,其中包含一些网址

我想收到每个人的网络服务器

但是当我使用get_headers时,我的输出将是:

(
    [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循环中插入代码,因为我写错了

我不知道如何从数组移动键值并将其复制到另一个数组

2 个答案:

答案 0 :(得分:1)

$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);

您可以在http://codepad.viper-7.com/nRle4F

看到回复(实时)

答案 1 :(得分:0)

只需将数组值添加到另一个数组中,同时循环遍历它们。像这样:

$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']值是否存在。