Any header is always seperated by two newlines, one to end the last line of header text and one to create an empty line before the body starts.
Here is a simple parser to rip apart the page by newline and then form an array containing the header and body elements.
You can play around with it to get it to actually parse teh header into name=>value pairs, but you didn't ask for it, and besides, that would take all the challenge out of that. I recommend extending it in such fashion as to allow for you to determine of there was a request error returned by the server.
But this code will get you going:
<?
$header = "hello open a socket please\n\n";
$request = "get me a cookie!\n";
$fp = fsockopen('www.webfreshener.com', 80, &$err_num, &$err_msg, 15);
if ($fp)
{
// Send everything
fputs($fp, $header . $request);
while (!feof($fp)) {
$response .= fgets($fp, 128);
}
}
//lets break it by newlines...
$parts = preg_split('/\n/',$response);
//here you can view the array....
print_r($parts);
//here we will loop through the array and compact back into a header and body.
// we start with the header...
$element = "header";
foreach($parts as $key=>$val) {
// once we find an empty line, we'll switch to the body
if (preg_match('/^[\s\t\n]$/',$val)) { $element = "body"; }
//here we compact into it's array section
$sections[$element] .= $val;
}
//and here it is...
print_r($sections);
?>