Then I stumbled across this function (from php.net) which somehow filters out the headers:
CODE
function sock_get_contents($ip, $port, $file, $timeout, $header = false)
{
$request = "GET ".$file." HTTP\1.0\r\n\r\n";
$return = '';
$sock = fsockopen($ip, $port, $err_no, $err_str, $timeout);
if(is_resource($sock))
{
fputs($sock, $request);
if($header == false)
{
$line = "";
while(!($line == "\r\n"))
{
$line = fgets($sock, 128);
if(preg_match("#Location\: (.*?)\\r\\n#si", $line, $matches))
{
return sock_get_contents($ip, $port, "/".$matches[1], $timeout, $header);
}
} // strip out the header
}
while ($buffer = fgets($sock, 4096))
{
$return .= $buffer;
}
fclose($sock);
return $return;
}
else
{
return false;
}
}
{
$request = "GET ".$file." HTTP\1.0\r\n\r\n";
$return = '';
$sock = fsockopen($ip, $port, $err_no, $err_str, $timeout);
if(is_resource($sock))
{
fputs($sock, $request);
if($header == false)
{
$line = "";
while(!($line == "\r\n"))
{
$line = fgets($sock, 128);
if(preg_match("#Location\: (.*?)\\r\\n#si", $line, $matches))
{
return sock_get_contents($ip, $port, "/".$matches[1], $timeout, $header);
}
} // strip out the header
}
while ($buffer = fgets($sock, 4096))
{
$return .= $buffer;
}
fclose($sock);
return $return;
}
else
{
return false;
}
}
The line with preg_match holds a regex which magically filters out all headers. My question is: how does it do it? It looks fiendishly complex and for the purpose of expanding my knowledge I wondered whether anyone could provide a short explanation.