Asked  7 Months ago    Answers:  5   Viewed   24 times

I've never used PHP but right now, I need to write a PHP file that displays in a log file the content of the body of a POST HTTP request.

I've read that you can access variables of the body via the _POST array. Unfortunately, it seems to be empty, although I'm pretty sure there is stuff in my HTTP request's body !

What should I use to be 100% sure of the content of my HTTP body ?

Thanks.

 Answers

63
$post_body = file_get_contents('php://input');

php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype="multipart/form-data".

(Source: http://php.net/wrappers.php)

Wednesday, March 31, 2021
 
Fernando
answered 7 Months ago
10

Well if you don't specify a db in the constructor, it uses the admin database by default (as documented in the Mongo::__construct-reference). Try the following:

$connection = new Mongo("mongodb://admin:adminpass@127.0.0.1/mydb");

notice the /mydb after the host-part, which lets the PHP-api connect to the desired database.

Wednesday, March 31, 2021
 
PandemoniumSyndicate
answered 7 Months ago
66

Problem appears to be specific to PhpStorm as issue can be reproduced on another machine running windows 10

Fix

When placing test.php and test2.php into my XAMPP 7.1 localhost directory, the issue does not appear, this also resolves the issue when placing files into WAMP directory on windows 10

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
86

First of all, if you are testing all the way from receiving the POSTed data to checking values in the database, this is not unit-test anymore : you are not testing one component in isolation of the others, but you are testing the integration of those components together.

It makes things harder to test :

  • you have to provide data in harder ways : not just as parameters to a method, but as parameters to your whole application (which means forging POST data, here, for instance)
  • you have more things to verify : not just the return value of a method, or if it threw an exception
  • you have several different and maybe unrelated things that can cause a failure (problem in some PHP code, problem in the database, database server not being available, ...), which will make failures harder to track down to find their cause.


Note that I didn't say that kind of "integration" tests is not useful, btw ;-)


Still, forging the $_POST array is quite simple : it is not read-only, and you can store whatever you want in it.

So, at the begining of your test-case, nothing prevents you from injecting any data you need in it.

Thursday, August 12, 2021
 
TMichel
answered 2 Months ago
14

You're passing the ReflectionClass an instance of B, which doesn't have access to $a. What you need is to pass it an instance of A instead. This should help clarify what you need to do here

class A
{
    private $a = 'Bob';
}

class B extends A
{
    function __construct()
    {
        $instance = new A();
        $reflection = new ReflectionClass($instance);
        $property = $reflection->getProperty('a');
        $property->setAccessible(true);
        echo $property->getValue(new A());
    }
}

(new B());

Demo

Saturday, August 28, 2021
 
TheCarver
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :