note 101786 modified in function.get-headers by danbrown
| From: | danbrown@php.net | Date: | Tue, 25 Jan 2011 22:26:42 +0000 |
| Subject: | note 101786 modified in function.get-headers by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176309@lists.php.net to get a copy of this message | ||
Unfortunately there is still no useful output format to handle redirects.
This function will bring all non-broken headers into a usable format. Too bad it has to call the
get_headers() funtion 2 times, but i dont see any other possibility right now.
<?php
function page_get_headers($crawl_uri) {
// get the array values where to split the second get headers return
$headers = get_headers($crawl_uri, 1);
if (empty($headers)) {
return array();
}
$splitmarks = array();
foreach ($headers as $key=>$h) {
if (is_numeric($key)) {
array_push($splitmarks, $h);
}
}
// get the "real" headers
$headers_final = array();
$i = 0;
$headers = get_headers($crawl_uri);
foreach ($headers as $head) {
// if the value is one of the splitmarks, start the next header
if (array_key_exists($i, $splitmarks) && $head === $splitmarks[$i]) {
$i++;
}
else {
// get the headers name
$tmp = explode(": ", $head, 2);
// check if the header is already existing, if yes, parse the similar header to an array
if (!empty($headers_final[$i-1][$tmp[0]])) {
if (is_array($headers_final[$i-1][$tmp[0]])) {
array_push($headers_final[$i-1][$tmp[0]], $tmp[1]);
}
else {
$headers_final[$i-1][$tmp[0]] = array($headers_final[$i-1][$tmp[0]], $tmp[1]);
}
}
else {
$headers_final[$i-1][$tmp[0]] = $tmp[1];
}
}
}
return $headers_final;
}
?>
--was--
Unfortunately there is still no useful output format to handle redirects.
This function will bring all non-broken headers into a usable format. Too bad it has to call the
get_headers() funtion 2 times, but i dont see any other possibility right now.
<?php
function page_get_headers($crawl_uri) {
// get the array values where to split the second get headers return
$headers = get_headers($crawl_uri, 1);
if (empty($headers)) {
return array();
}
$splitmarks = array();
foreach ($headers as $key=>$h) {
if (is_numeric($key)) {
array_push($splitmarks, $h);
}
}
// get the "real" headers
$headers_final = array();
$i = 0;
$headers = get_headers($crawl_uri);
foreach ($headers as $head) {
// if the value is one of the splitmarks, start the next header
if ($head === $splitmarks[$i]) {
$i++;
}
else {
// get the headers name
$tmp = explode(": ", $head, 2);
// check if the header is already existing, if yes, parse the similar header to an array
if (!empty($headers_final[$i-1][$tmp[0]])) {
if (is_array($headers_final[$i-1][$tmp[0]])) {
array_push($headers_final[$i-1][$tmp[0]], $tmp[1]);
}
else {
$headers_final[$i-1][$tmp[0]] = array($headers_final[$i-1][$tmp[0]], $tmp[1]);
}
}
else {
$headers_final[$i-1][$tmp[0]] = $tmp[1];
}
}
}
return $headers_final;
}
?>
http://php.net/manual/en/function.get-headers.php