Hi!
I am trying to get a little bit less workload with the PostToHost-function.
Here is what I want to do: When someone signs up for my site and indicates that we wants to get the newsletter, I need to post the data the user submitted to two different hosts:
a) to my database
b) to topica.com
Right now I'd have to get the data I got by E-Mail (which is only an e-mail-address) and enter it into a form from topica manually.
So, here is my code, which isn't working:
<?
function PostToHost($host, $path, $data) {
$fp = fsockopen($host,80);
printf("Open!\n");
fputs($fp, "POST $path HTTP/1.1\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: ".strlen($data)."\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, "$data\n");
printf("Sent!\n");
while(!feof($fp)) {
$res .= fgets($fp, 128);
}
printf("Done!\n");
fclose($fp);

return $res;
}

$data = "?digest=f&Submit=Submit&sublist=dummy@online-talks.de&invite=OPTOUT";

printf("Go!\n");
$x = PostToHost(
"www.topica.com",
"/lists/online-talks.de-english/importsub.pl",
$data
);
?>
Here is the data I see on my topica-form (the one where I enter data by myself usually):

<HEAD><TITLE></TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="/lists/online-talks.de-english/importsub.pl" ENCTYPE="multipart/form-data">
Addresses:<TEXTAREA NAME="sublist" ROWS=5 COLS=40 WRAP=PHYSICAL></TEXTAREA><br>
<INPUT TYPE=RADIO NAME=invite VALUE="OPTOUT" CHECKED>Add Directly to List
<SELECT NAME=digest>
<OPTION VALUE='F'>Individual Messages
</SELECT>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>

I hope someone can help. Thanks in advance.🙂
Ciao
Sascha

    2 months later

    Try to replace
    fputs($fp, "$data\n");
    by
    fputs($fp, "$data");

    and

    $data = "?digest=f&Submit=Submit...
    by
    $data = "digest=f&Submit=Submit...

    Tell me if it works!

    U.Tollas@xantho.de

      22 days later

      try removing the line:

      fputs($fp, "Host: $host\n");

        6 months later

        Hi.

        I've came across with the problem as well and I wonder if you have the solution already ?

        What I want to do is to invoke a php script from another server via http post with some variables passing to it. Let me show you my script, I'm using what sascha has written :

        <?
        filename: posttohost.php

            function PostToHost($host, $path, $data) 
            {
                    $fp = fsockopen($host,80);            
                    printf("Open!\n");            
                    fputs($fp, "POST $path HTTP/1.1\n");            
                    fputs($fp, "Host: $host\n");            
                    fputs($fp, "Content-type: application/x-www-form-urlencoded\n");            
                    fputs($fp, "Content-length: ".strlen($data)."\n");            
                    fputs($fp, "Connection: close\n\n");            
                    fputs($fp, "$data");              
                    printf("Sent!\n");            
        
                    while(!feof($fp)) 
                    {
                            $res .= fgets($fp, 128);    
                    }            
                    printf("Done!\n");            
                    fclose($fp);            
        
                    return $res;            
            }                    
        
            $data = "login=bbbear&var=abctesting";                                                          
        
            printf("Go!\n");                    
            $x = PostToHost("192.xxx.xx.xxx", "/home/path/to/the/script.php", $data);                                 ?>        

        And here's the content of the script.php:

        <?
        //To create a new file with the data that passed from other server

           srand((double)microtime()*1000000);
            $file = rand();
            $ar_var[0] = $login;
            $ar_var[1] = $var;
            $str = join($ar_var, ";");
        
            $fp = fopen($file, "w");
            fwrite($fp, $str, strlen($str));
            fclose($fp);

        ?>

        When I run the script "posttohost.php", the browser shows this line "Go! Open! Sent! Done!" but there's no new file being created on the server that I've given to the function. Have I missed anything ?

        Anyway, is there any other alternatives that can be adopted for "server-to-server talk" kind of application ?

        Thank you very much.

          Write a Reply...