Wednesday 6 May 2009

View the raw HTTP request body in PHP

I'm currently working on a bug where some Japanese characters are not appearing correctly on the FreeFixer web site. In the process of tracking down the problem I needed to view the raw HTTP request body that FreeFixer sends when posting information about a file or some registry data. In PHP, this can be done by calling the http_get_request_body() function. Unfortunately, this function requires a PECL extension which was unavailable in my current setup. However, this solved the problem:

$request_body = @file_get_contents('php://input');

5 comments:

  1. Gorgeous! Exactly what I needed. Thank you very much indeed.

    ReplyDelete
  2. I was also looking for this! It made the trick! Thanks a lot.

    ReplyDelete
  3. Wrong.

    Use this instead:

    $request_body = @file_get_contents(STDIN);

    php://input can only be read once and does not support seek operations. Also php://input can not read the body if a form is posted with enctype="multipart/form-data".

    You can read more here:
    http://php.net/manual/en/wrappers.php.php

    The STDIN constant should be used in place of 'php://stdin'.

    ReplyDelete
    Replies
    1. I've found that for purposes of reading xml submissions to a controller function, php://input works just fine since you only have to read it once (like push notifications)

      Delete