note 84957 deleted from function.get-headers by danbrown
| From: | danbrown@php.net | Date: | Mon, 09 Jan 2012 19:57:49 +0000 |
| Subject: | note 84957 deleted from function.get-headers by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185492@lists.php.net to get a copy of this message | ||
Note Submitter: Radon8472
----
No, it doesn`t.. It is a bad Idea to searczh for the text "Moved" if you want to find out
if its a redirect.
Better is to look for the Responscode 301,302 and 303.
<?php
function get_headers($Url, $Format= 0, $Depth= 0) {
if ($Depth > 5) return;
$Parts = parse_url($Url);
if (!array_key_exists('path', $Parts)) $Parts['path'] = '/';
if (!array_key_exists('port', $Parts)) $Parts['port'] = 80;
if (!array_key_exists('scheme', $Parts)) $Parts['scheme'] =
'http';
$Return = array();
$fp = fsockopen($Parts['host'], $Parts['port'], $errno, $errstr, 30);
if ($fp) {
$Out = 'GET '.$Parts['path'].(isset($Parts['query']) ?
'?'.@$Parts['query'] : '')." HTTP/1.1\r\n".
'Host: '.$Parts['host'].($Parts['port'] != 80 ?
':'.$Parts['port'] : '')."\r\n".
'Connection: Close'."\r\n";
fwrite($fp, $Out."\r\n");
$Redirect = false; $RedirectUrl = '';
while (!feof($fp) && $InLine = fgets($fp, 1280)) {
if ($InLine == "\r\n") break;
$InLine = rtrim($InLine);
list($Key, $Value) = explode(': ', $InLine, 2);
if ($Key == $InLine) {
if ($Format == 1)
$Return[$Depth] = $InLine;
else $Return[] = $InLine;
if ((strpos($InLine, '301') > 0)||(strpos($InLine, '302')
> 0)||(strpos($InLine, '303') > 0) ) $Redirect = true;
} else {
if ($Key == 'Location') $RedirectUrl = $Value;
if ($Format == 1)
$Return[$Key] = $Value;
else $Return[] = $Key.': '.$Value;
}
}
fclose($fp);
if ($Redirect && !empty($RedirectUrl)) {
$NewParts = parse_url($RedirectUrl);
if (!array_key_exists('host', $NewParts)) $RedirectUrl =
$Parts['host'].$RedirectUrl;
if (!array_key_exists('scheme', $NewParts)) $RedirectUrl =
$Parts['scheme'].'://'.$RedirectUrl;
$RedirectHeaders = get_headers($RedirectUrl, $Format, $Depth+1);
if ($RedirectHeaders) $Return = array_merge_recursive($Return, $RedirectHeaders);
}
return $Return;
}
return false;
}
?>