Closed Thread Icon

Topic awaiting preservation: Separating HTTP headers from content (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12448" title="Pages that link to Topic awaiting preservation: Separating HTTP headers from content (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Separating HTTP headers from content <span class="small">(Page 1 of 1)</span>\

 
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-25-2002 22:41

I know this is going to be one of those slap myself in the forehead and say duh when I hear the answer, but I've been trying to figure this out for a while and I'm just not seeing it.

I'm writing a script that will take a URL and send it with a header like so:

code:
<?php
$fp = fsockopen ("test.domain.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET /test.html HTTP/1.1\r\nHost: test.domain.com\r\nIf-Modified-Since:
Fri, 15 Aug 2002 22:18:59 GMT \r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>


Which works fine. My problem starts when the file has been modified and the server sends a new file. I comes with the headers and then the content and I'm not sure of a fullproof way to separate the content from the headers to display it.

Also, when I run this code in a browser, it seems to take an unusually long time. Can anybody see any faults in my code that would be causing that.

Thanks!

-Butcher-

[edit: fixed HSBoD]

[This message has been edited by Emperor (edited 09-26-2002).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-26-2002 01:39

It would depend on the type of file - with a HTML file you could just grab everything between the HTML tags but as you are looking at XML/RSS then you could grab everything after the opening XML tag. So if you were to put it all in a string ($input):

code:
eregi("<?xml (.*)", $input, $output);



but you'd have to put that back on:

$output = '<?xml ' . $output;

but messing around with the <?xml bit can make PHP unhappy so it you could guarantee what the last bit of the headers were you could grab everything after that. Anyway a bit of fiddling (and possibly the use of a Here Document formatted string for the <?xml) should provide you with the formula you require.

[edit: the code you posted over at DevShed would allow you to do this a little easier as it gets it into a string:

code:
while (!feof($fp)) {
$input .= fgets ($fp,2280);
}
fclose ($fp);
eregi("<?xml (.*)", $input, $output);
ETC



___________________
Emps

FAQs: Emperor

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-26-2002 02:58

For those wondering what a Here Document is (like butcher ):
http://faq.ozoneasylum.com/798/

___________________
Emps

FAQs: Emperor

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-26-2002 10:25

Butcher, as far as i remember (<-- AFAIR ? ) , the header and the content are seperated by two \n\r or something like that.


//lallous

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-26-2002 13:31

Thanks lallous, I wasn't sure if that was done universally enough to rely on, but I guess it's really the standard.

I'll give it a try.

-Butcher-

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-26-2002 14:16

And...

Thank you Emperor for all your help!

Your always a boon of information no matter what I'm trying to accomplish.

-Butcher-

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-26-2002 14:24

lallous: Great idea. I think it might be:

\r\n\r\n

___________________
Emps

FAQs: Emperor

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-26-2002 15:17

butcher, check this out: http://www.sloppycode.net/sloppycode/PHP/cs17.html

code:
function _stripResponseHeader($source)
{
$headerend = strpos($source,"\r\n\r\n");
if (is_bool($headerend))
{
$result = $source;
} else{
$result = substr($source,$headerend+4,strlen($source) - ($headerend+4));
}
return $result;
}



[This message has been edited by lallous (edited 09-26-2002).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-26-2002 17:06

Cool!!

Thanks lallous, that looks like exactly what I need!!

And thanks for the link.

-Butcher-

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-26-2002 20:08

Okay, one last question on this.

If I open a socket and send this header with HTTP1.1

code:
fputs ($fp, "GET /cgi-local/page?c=Jokes&o=xml_1 HTTP/1.1\r\nHost: p.moreover.com If-Modified-Since: Sun, 22 Sep 2002 07:48:00 GMT\r\n\r\n");

I get some extra characters returned that are after the \r\n\r\n and just before the content starts. There is also a 0 at the end of the results.

But when I send the same header as a 1.0 header the return is just fine? Should I just not worry about it and use the 1.0 version or am I doing something wrong in the 1.1 header.

And lallous... That bit of code worked perfectly!!

Thanks!

-Butcher-

« BackwardsOnwards »

Show Forum Drop Down Menu