Page 1 of 1

fsockopen cannot receive post data on localhost

Posted: Fri May 01, 2009 12:21 pm
by Perseas
Hi i am relative new to php,
on my own apache server (localhost), i use one .php script to 'post' data to another .php script also on my server. The problem is that although the http request reaches the receiving script, it seems that there is no posted data.

The code on the sending page is:

Code: Select all

<?php
 
$req = "&One=Ena&Two=Dyo&Three=Tria";
$req = urlencode($req);
$header = "POST /main/php/Receive.php HTTP/1.1\r\n";
$header .= "Host: localhost\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header .= $req;
 
 
$fp = fsockopen ('localhost', 8000, $errno, $errstr,2);
             if (!$fp) { $message ="// HTTP connection ERROR!"; echo $message; exit;}
             else {
                  fputs ($fp, $header);
                  echo "Success on fputs!<br/>";
 
                  while (!feof($fp) )
                      {
                       $res[$i] = fgets($fp,1024);
                       echo "RESULTs :".count($res)."<br/>";
                       echo 'Res['.$i.'] ='.$res[$i]."<br/>";
                       $i++;
                      }
                 }
fclose($fp);
 
?>

while the code on the receiving script is:

Code: Select all

<?php
 
$result = 'NULL';
 
foreach( $_POST as $key => $value ) // also tried $_REQUEST
                { $result .= $value; }
///// then i write the $result on a MYSQL table
 ?>
On each execution of the sending script, the receiving script is triggered. I know this, because a new insertion of $result is entered in mysql, but it is allways an emty string.
I checked the $_POST with other ways also, but it still is empty.
Please advise what am i doing wrong. Any help appreciated.