phpurl-routingsuperglobalsphp-builtin-server

PHP builtin server/routing script: Empty superglobals


I'm trying to start a local server that catches all Urls called. Basically I want to simulate a REST-Interface for testing purposes, and write all Urls and the POST/PUT/PATCH Data to a file.

I start the server like this:

php -S localhost:9999 -t /var/www/tests/import/ /var/www/tests/import/index.php

/var/www/tests/import/ is the working directory, /var/www/tests/import/index.php is the routing script that gets called for every Url that is called.

The source code of index.php is this:

$path = __DIR__ . '/output/test.txt';

file_put_contents($path, 'POST:' . var_export($_POST) . PHP_EOL, FILE_APPEND);
file_put_contents($path, 'SERVER:' . var_export($_SERVER) . PHP_EOL, FILE_APPEND);
file_put_contents($path, 'REQUEST-URI: ' . var_export($_SERVER["REQUEST_URI"]) . PHP_EOL, FILE_APPEND);
file_put_contents($path, 'PAYLOAD: '. file_get_contents('php://input') . PHP_EOL, FILE_APPEND);
file_put_contents($path, 'ENV: '. var_export($_ENV) . PHP_EOL, FILE_APPEND);
file_put_contents($path, 'RQUEST: '. var_export($_REQUEST) . PHP_EOL, FILE_APPEND);

The output of the script looks like this:

POST:
SERVER:
REQUEST-URI: 
PAYLOAD: {"success":true,"duration":10.624989032745361,"error":null}
ENV: 
RQUEST: 

So, the script does get called, however all the superglobals are empty and the only thing that gets written to my file is the Payload of the request. Now I only want to know what URL was called, how can I archieve that?

Settings should be standard for Ubuntu 18, this the variables order setting in php.ini:

variables_order = "GPCS"

Solution

  • In order to return a value (and not output) from var_export(), the second argument needs to be true; e.g:

    echo var_export(range(1, 3), true);
    

    Yields:

    array (
      0 => 1,
      1 => 2,
      2 => 3,
    )
    

    Hope this helps