Re: How to avoid Socket Post output?

From: Date: Mon, 16 Jun 2003 21:17:18 +0000
Subject: Re: How to avoid Socket Post output?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-151728@lists.php.net to get a copy of this message
On Mon, 2003-06-16 at 13:46, Kevin Stone wrote: > Hello list, > > I'm attempting to use the PostToHost() function to send a request to a > remote script. The purpose for this is to request and retrieve information > from a remote database without the need for messy HTTP & HTML methods. I > realize that I could use an HTML form, put the data in hidden fields, and > send the request that way, but then I would have to redirect back from the > master script recording an unwanted page in the 'Back Button' history. I'd > like to avoid that so I'm investigating methods of POST'ing through a manual > socket connection. > > Here is that PostToHost() function.. > > /********************************************************* > PostToHost($host, $path, $data_to_send) > $host; valid domain (ie. www.domain.com) > $path; relative URL (ie. /myfile.php) > $data_to_send; urlencoded key/val string (ie. > "key=val&key2=val2&key3=val3") > *********************************************************/ > function PostToHost($host, $path, $data_to_send) > { Add here: $ret_string = ''; > ob_end_flush(); > $fp = fsockopen($host,80); > fputs($fp, "POST $path HTTP/1.0\n"); > fputs($fp, "Host: $host\n"); > fputs($fp, "Content-type: application/x-www-form-urlencoded\n"); > fputs($fp, "Content-length: " . strlen($data_to_send) . "\n"); > fputs($fp, "Connection: close\n\n"); > fputs($fp, $data_to_send); > while(!feof($fp)) > { > echo fgets($fp, 128); Replace the echo line above with: $ret_string .= fgets($fp, 2048) . "\n"; > } > fclose($fp); Add here: return $ret_string; > } > > > It works great! The only problem is I get output from the socket connection > and I don't know how to skip over it... > > HTTP/1.1 200 OK > Date: Mon, 16 Jun 2003 20:10:36 GMT > Server: Apache/1.3.20 (Unix) ApacheJServ/1.1.2 PHP/4.2.3 > FrontPage/5.0.2.2510 Rewrit/1.1a > X-Powered-By: PHP/4.2.3 > Connection: close > Content-Type: text/html > > I'm very much a newbie to all of this so you'll have to forgive me. But I'm > wondering if there is anyway to avoid this output and have a completely > invisible manual socket POST? I'm assuming with the above that you want to get the returned text from the POST into a string for use instead of outputting it directly to the browser; these changes should do that. Note: I haven't actually tested it. :) Good luck, Torben > Thanks, > Kevin Stone > kevin@asiostudio.com -- Torben Wilson <torben@php.net> +1.604.709.0506 http://www.thebuttlesschaps.com http://www.inflatableeye.com http://www.hybrid17.com http://www.themainonmain.com -----==== Boycott Starbucks! http://www.haidabuckscafe.com ====-----

« previous php.general (#151728) next »