<?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