0

I am using fsockopen to connect and send data to a script. The problem is that no data is received on the receiving end as you can see in my output below, only empty array is printed. I've based my solution on Tamlyns answer here: PHP Post data with Fsockopen. I did' try his way of creating the post-parameters, no difference in the output.

My main script:

<?php
session_start();  

    $fp = fsockopen("192.168.1.107",    
          80,
          $errno, $errstr, 10);    

    $params = "smtp=posteddata\r\n";
    $params = urlencode($params);

    $auth = base64_encode("kaand:kaand123");

     if (!$fp) {
          return false;
      } else {
          error_log("4");
          $out = "POST /smic/testarea/fsockopen_print_post.php HTTP/1.1\r\n";      
          $out.= "Host: 192.168.1.107\r\n";      
          $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
          $out.= "Authorization: Basic ".$auth;
          $out.= 'Content-Length: '.strlen($params).'\r\n';
          $out.= "Connection: Close\r\n\r\n";
          $out .= $params;

          fwrite($fp, $out);
          fflush($fp);

          header('Content-type: text/plain');
          while (!feof($fp)) {
                echo fgets($fp, 1024);
            }

          fclose($fp);
      }        
?>

fsockopen_print_post.php:

<?php
session_start();

print_r($_POST);

$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];  
parse_str( $raw_data, $_POST );
print_r($_POST);
?>

Output:

HTTP/1.1 200 OK
Date: Mon, 19 Mar 2012 09:21:06 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.10
Set-Cookie: PHPSESSID=i4lcj5mn1ablqgekb1g24ckbg5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 20
Connection: close
Content-Type: text/html; charset=UTF-8

Array
(
)
Array
(
)

What's the problem and how do I fix it?

Community
  • 1
  • 1
Nicsoft
  • 3,644
  • 9
  • 41
  • 70

2 Answers2

2

There's a typo in your code:

$out.= 'Content-Length: '.strlen($params).'\r\n';
$out.= "Connection: Close\r\n\r\n";

Should be:

$out .= "Content-Length: ".strlen($params)."\r\n";
$out .= "Connection: close\r\n\r\n";

See how you've used single quotes around \r\n? That means literally send '\r\n' rather than interpret these as a Windows crlf. The double quotes corrects this.

deed02392
  • 4,799
  • 2
  • 31
  • 48
  • Thanks for your answer. Is this really necessary? Checking the examples for fsockopen, they don't do antything like this ([php.net](http://se2.php.net/manual/en/function.fsockopen.php)) and for all other examples I have found, this is not used anywhere. I just want to post the data and then disconnect (should be in an asynchronous manner). – Nicsoft Mar 19 '12 at 10:19
  • It's necessary if you want to be able to process multiple clients without blocking. If you 'know' you'll only need one, which often means you do now but will need more in the future, there is a simpler way. But I suggest you implement the solution this way to save yourself a headache in the future. Besides, you stand to lose nothing and yet gain better knowledge when you figure out how this works. ;) – deed02392 Mar 19 '12 at 10:24
  • The learning curve is quite steep anyways now ;) Perhaps this helps processing multiple clients, but does this help me getting the posted data on the receiving end which is what I asked for? – Nicsoft Mar 19 '12 at 10:26
  • Oh sorry, it's been a slow morning (early Monday here). It's probably because of your typos. See my answer edit! – deed02392 Mar 19 '12 at 10:35
  • Thanks, actually, I did update that already and it didn't help. Perhaps it would have been a problem as well after solving it according to my own answer... – Nicsoft Mar 19 '12 at 11:55
  • It would have been :) Well spotted, sorry I missed that. – deed02392 Mar 19 '12 at 12:00
  • And actually, I changed back to single quotes just to check, and indeed I did get some errors. – Nicsoft Mar 19 '12 at 12:12
  • Yes as I expected, you would be sending literally a backslash then an 'r' etc rather than proper characters representing a linefeed and carriage return. – deed02392 Mar 19 '12 at 12:37
0

This row

 $out.= "Authorization: Basic ".$auth;

should have been

 $out.= "Authorization: Basic ".$auth."\r\n";

There was something else which I haven't identified, but this code works (probably just some mixing up of variables when testing around):

<?php
$fp = fsockopen('192.168.1.107', 80);
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
$auth = base64_encode("kaand:kaand123");
$out = "POST /smic/testarea/fsockopen_print_post.php HTTP/1.1\r\n";      
$out .= "Host: 192.168.1.107\r\n";      
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Authorization: Basic ".$auth."\r\n";
$out .= "Content-Length: ".strlen($content)."\r\n";
$out .= "Connection: close\r\n\r\n";
$out .= $content;

fwrite($fp,$out);

header("Content-type: text/plain");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);    
?>
Nicsoft
  • 3,644
  • 9
  • 41
  • 70