RE: [PHP] Faking a form POST
| From: | .ben | Date: | Tue, 16 Apr 2002 14:50:52 +0000 |
| Subject: | RE: [PHP] Faking a form POST | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-93201@lists.php.net to get a copy of this message | ||
Erik, thanks for this. Works great,
.b
> -----Original Message-----
> From: Erik Price [mailto:pricee@hhbrown.com]
> Sent: 16 April 2002 14:30
> To: ben@babelfish.co.uk
> Cc: PHP
> Subject: Re: [PHP] Faking a form POST
>
>
>
> On Monday, April 15, 2002, at 09:36 PM, .ben wrote:
>
> > Hi. Does anyone know how to POST data to another script as if a form
> > had
> > done so? I want to POST some data to a script on another site and
> > retrieve
> > the data that is returned so i can poick it apart, i did it in ASP
> > using a
> > 3rd party component... just wondering if the same could be done in PHP.
> >
> > Any help appreciated.
>
> This function will look a lot prettier once you've copied it into a text
> editor that can handle > 80 columns -- or you can remove the comments.
>
> From the archives:
>
> # ===============================================================
> # PostToHost($host, $path, $data_to_send)
> # ---------------------------------------------------------------
> # "It is a trivial little function."
> # -Rasmus
> # ===============================================================
>
> function PostToHost($host, $path, $data_to_send)
> {
> $fp = fsockopen($host,80);
> // $fp now points
> to the "file" opened
> by fsockopen
> fputs($fp, "POST $path HTTP/1.0\n");
> // write the first header, with
> the path and the protocol (annotations suggest using 1.0 over 1.1)
> fputs($fp, "Host: $host\n");
> // write the hostname line
> of the header
> fputs($fp, "Content-type:
> application/x-www-form-urlencoded\n"); // write
> the encoding type line of the header
> fputs($fp, "Content-length: " . strlen($data_to_send) .
> "\n"); // write
> the content-length of data to send
> fputs($fp, "Connection: close\n\n");
> // close the connection, and
> a blank line
> fputs($fp, $data_to_send);
> // write the data
> to send (in POST variable
> form)
> while(!feof($fp)) {
> // until
> the end of the "file", eat 128
> bytes at a time
> echo fgets($fp, 128);
> }
> fclose($fp);
> // close the "file"
> }
>
>
>
> ----
>
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> pricee@hhbrown.com
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>