<?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 request和the Curl response处理程序。
它比您对“grep”命令所做的更复杂一些,但看起来好像您要分析响应头。这就是为什么我在代码中留下更复杂的标题分析的原因。
您是否试过阅读[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
我无法真正弄清楚这就是为什么我来到这里@Leigh – 2012-02-07 11:22:59
至少告诉我们你的尝试。 – Leigh 2012-02-07 11:24:12