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 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历