Closed Thread Icon

Topic awaiting preservation: Removing Headers from a Server Response in PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27437" title="Pages that link to Topic awaiting preservation: Removing Headers from a Server Response in PHP (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Removing Headers from a Server Response in PHP <span class="small">(Page 1 of 1)</span>\

 
divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 02-03-2006 17:23

Hi folks,

I'm retrieving a file via POST in PHP using fsockopen(), and assigning the data to a variable with fgets(). However, when I echo the data to the browser, it echoes the headers in addition to the data. Is there an easier and more effective way of removing or otherwise suppressing headers on a remote file? I've tried searching, but most of what I've found involves using mod_header to remove or suppress your own headers, not a native PHP method. I could use a regex to find and extract the HTML, but would prefer a more reliable and suitable method.

(Btw, I'm using fsockopen because I don't want it dependent on external libraries, so using libcurl isn't an option.)

Cheers,
DC

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2006 01:19

Try this

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;
}



- Butcher -

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-04-2006 10:58

All HTTP Servers *must* return headers and seperate them from the body by a double link break - just as butcher parses in his code.

So, it'd be fine to skip that if clause and just use the substr... btw, I always use $headerend === false - that's a little more natural, though is_bool() surely would work as well.

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 02-04-2006 11:14

I instead used:

code:
function stripHeader($source) {
	list($header,$content) = explode("\r\n\r\n",$source);
	return $content;
}



Which performs ostensibly the same check (tmk -- if there is a caveat to the above, please let me know). I had been trying to preg_match_all() with "/<!DOCTYPE(.*)<\/html>/i", but as you can tell, my regex needs a lot of work.

Thank you both for all of your help.

Regards,
DC

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-04-2006 14:26

You're code will fail if there's any double line break in the body, and there's nothing in the spec that will prevent that.
You need to split on the first double link break. Not to mention you're waisting processor time looking for further double line breaks ;-).

You can not use a regexps to match for doctype - many many sites on the web still don't have a doctype in their HTML, and it's no good if you're retrieving a text file, or a pdf, or a soap xml message.

So forget all about that.
Use the code butcher provided ;-)

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 02-04-2006 19:40

Thanks, Tyberius. I only need this response from a particular website which does have a doctype, but you're right about looking for doctypes more generally. What about the code below? explode() allows a limit parameter, which should (a) keep from wasting processor cycles and (b) splitting additional line breaks?

code:
function stripHeader($source) {
	list($header,$content) = explode("\r\n\r\n",$source,2);
	return $content;
}



Thanks,
DC

Edit: "keep wasting" -> "keep from wasting". Bad typo.

(Edited by divinechaos on 02-04-2006 20:08)

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-06-2006 09:57

Still... Why would the limit be two and not one?

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 02-06-2006 23:40
quote:
If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

-- PHP Manual

Limit is the number of elements in the returned array, not the number of delimiters. So, because I want two elements in the return array, I used 2 and not 1.

Thanks for all of your help, Tyberius. It's very much appreciated.

Cheers,
DC

« BackwardsOnwards »

Show Forum Drop Down Menu