2012-02-07 60 views
0

这怎么能在PHP做到这一点卷曲在PHP

curl -I http://www.google.com | grep "Server:" 

来完成,并有可能只是回声操作系统,如Linux或站点的窗户,像Web服务器是呼应使用curl

我尝试

<? 
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL,"http://google.com"); 
curl_setopt ($ch, CURLOPT_HEADER, 1); 
$data = curl_exec ($ch); 
curl_close ($ch); 
echo $data ; 

?> 
+1

您是否试过阅读[PHP cURL文档](http://php.net/manual/en/book.curl.php)或[PHP模式匹配文档](http://php.net/manual /en/function.preg-match.php)? – Leigh 2012-02-07 11:14:52

+0

我无法真正弄清楚这就是为什么我来到这里@Leigh – 2012-02-07 11:22:59

+0

至少告诉我们你的尝试。 – Leigh 2012-02-07 11:24:12

回答

0
<?php 
// create a new cURL resource                                 
$ch = curl_init(); 

// set URL and other appropriate options                              
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/"); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// grab URL and pass it to the browser                              
$result = curl_exec($ch); 

/* Curl returns multiple headers, if the last action required multiple                      
* requests, e.g. when doing Digest authentication. Only parse the                       
* headers of the latest response. */ 
preg_match_all('/(^|\r\n\r\n)(HTTP\/)/', $result, $matches, PREG_OFFSET_CAPTURE); 
$startOfHeaders = $matches[2][count($matches[2]) - 1][1]; 
$endOfHeaders = strpos($result, "\r\n\r\n", $startOfHeaders); 
$headers = substr($result, $startOfHeaders, $endOfHeaders - $startOfHeaders); 
$headers = preg_split("/\r?\n/", $headers); 
foreach ($headers as $headerLine) { 
    if (preg_match('|^Server:\s+(.+)|', $headerLine, $m)) { 
    var_dump($m[1]); 
    } 
} 

// close cURL resource, and free up system resources                           
curl_close($ch); 

基本上这是从the Horde_Http library的提取物。特别是the Curl requestthe Curl response处理程序。

它比您对“grep”命令所做的更复杂一些,但看起来好像您要分析响应头。这就是为什么我在代码中留下更复杂的标题分析的原因。

+0

是的,这就是我要找的:)谢谢! – 2012-02-07 11:34:48

+0

关于我提到的第二个问题,你可以说些什么? – 2012-02-07 11:36:33

+0

识别操作系统并不可靠。但是通常Web服务器的默认设置将允许您提取操作系统。请参阅[链接](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.38) – 2012-02-07 12:03:04