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

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"
  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理