simplfuzz
simplfuzz

Reputation: 12935

Reading HttpRequest sent by Java in PHP

PostMethod postMethod = new PostMethod("http://abc.com/a.php");
postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(someString.getBytes())));
HttpClient httpClient = initMultithreadedHttpClient(ConnectionTimeout,
                SocketTimeout, MaxRetry);
httpClient.executeMethod(postMethod);

This is how I am sending data from Java client to PHP server.
How do I read it in PHP?

I tried to capture the data like this:

<?php
$fp = fopen("/opt/lampp/htdocs/input.txt","w");
ob_start();
print_r($_REQUEST);
print_r($_SERVER);
print_r(http_get_request_body());
fprintf($fp,"%s",ob_get_contents());
ob_end_clean();
fclose($fp);
?>

But it didn't really print the request data.

Upvotes: 0

Views: 251

Answers (2)

deadrunk
deadrunk

Reputation: 14153

Try this function instead of http_get_request_body:

function get_post_body() {
  $body = '';
  $fh   = @fopen('php://input', 'r');
  if ($fh)
  {
    while (!feof($fh))
    {
      $s = fread($fh, 1024);
      if (is_string($s))
      {
        $body .= $s;
      }
    }
    fclose($fh);
  } 
  return $body
}

Upvotes: 1

alex
alex

Reputation: 490647

You can read raw POST data in PHP with...

$post = file_get_contents('php://input');

I prefer this method over the $HTTP_RAW_POST_DATA global.

Upvotes: 0

Related Questions