doukuangxun5382
2018-10-16 09:33
浏览 82
已采纳

PHP内置服务器/路由脚本:空的超级全局

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"

图片转代码服务由CSDN问答提供 功能建议

我正在尝试启动一个捕获所有被调用Url的本地服务器。 基本上我想模拟REST接口用于测试目的,并将所有Url和POST / PUT / PATCH数据写入文件。

我像这样启动服务器:

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

/ var / www / tests / import /是工作目录,/ var / www / tests / import / index.php是为每个被调用的Url调用的路由脚本。

index.php的源代码是:

  $ path = __DIR__。  '/output/test.txt'; 
nnn_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:  //输入')。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); 
   
 
 

脚本的输出如下所示:

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

因此,脚本会被调用,但是所有的superglobals都是空的,并且唯一被写入我的文件的是请求的有效负载。 现在我只想知道调用了什么URL,我该如何实现?

设置应该是Ubuntu 18的标准设置,这是php.ini中的变量顺序设置:

  variables_order =“GPCS”
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpy3846 2018-10-16 09:35
    已采纳

    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

    点赞 打赏 评论

相关推荐 更多相似问题