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
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to