Web Hosting Talk







View Full Version : HowTo Suppress HTTP Headers


rrdega
01-28-2004, 11:04 AM
G'Morning,

First let me say that these three functions (fsockopen & fputs & fgets) are fairly new to me. So this may be really simple, and the solution is just evading me...

I am using this function which I scrounged up for POSTing data:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* sendToHost
* ~~~~~~~~~~
* Params:
* $host - Just the hostname. No http:// or /path/to/file.html portions
* $method - get or post, case-insensitive
* $path - The /path/to/file.html part
* $data - The query string, without initial question mark
* $useragent - If true, 'MSIE' will be sent as the User-Agent (optional)
*
* Examples:
* sendToHost('www.google.com','get','/search','q=php_imlib');
* sendToHost('www.example.com','post','/some_script.cgi',
* 'param=First+Param&second=Second+param');
*/
function sendToHost($host,$method,$path,$data,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == 'GET') {
$path .= '?' . $data;
}
fputs($fp, "$method $path HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $data);
}
while (!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
return;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Works Great! Except the "page" that is output to the browser includes the HTTP Headers. Like so:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP/1.1 200 OK Date: Wed, 28 Jan 2004 02:58:42 GMT Server: Apache/1.3.28 (Unix) mod_auth_passthrough/1.8 mod_gzip/1.3.26.1a mod_log_bytes/1.2 mod_bwlimited/1.2 FrontPage/5.0.2.2634 mod_ssl/2.8.15 OpenSSL/0.9.6b PHP-CGI/0.1b X-Powered-By: PHP/4.3.3 Vary: Accept-Encoding Connection: close Content-Type: text/html
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there an easy way to suppress the display of these headers?

Thanx!

CyberAlien
01-28-2004, 11:56 AM
put it all in string instead of echoing, then split to header/footer like this:

list($header, $data) = explode("\r\n\r\n", $page, 2);

and then echo $data;

rrdega
01-28-2004, 12:12 PM
Coolest of all Beans! I'll give a shot right now!! Will report back shortly...

:D

rrdega
01-28-2004, 12:31 PM
You da Man, CyberAlien!!! Worked like a Champ! I literally took your code snippets, (removing 'and then'), munged the function a tad to return a '$buf' and it works Lickity-Spilt!

Many Thanx!