Moh*_*ati 24 php embeddedwebserver
在python内置Web服务器中使用print函数时,它会在终端中打印结果...
例如:
Django version 1.3.4, using settings 'parsicore.settings'
Development server is running at http://0.0.0.0:8000/
Using the Werkzeug debugger (http://werkzeug.pocoo.org/)
Quit the server with CONTROL-C.
127.0.0.1 - - [16/Jan/2013 02:02:08] "GET / HTTP/1.1" 200 -
hello ... print 1 2 3 
如何在PHP内置Web服务器中打印这样的内容?
例如,我想在终端打印$ _POST.我php -S 127.0.0.1:3000用于运行PHP内置的Web服务器.
Cha*_*les 29
内置于PHP 5.4+的开发Web服务器无法以您希望的方式工作.也就是说,它不是一个PHP进程,你不能让它为你运行代码.
它旨在为指定目录中的PHP应用程序和内容提供服务.服务器进程的输出是访问日志.您可以使用error_log函数写入日志,值为4 message_type.所以,理论上,你可以做类似的事情
ob_start();
var_dump($_POST);
error_log(ob_get_clean(), 4);
听起来你正试图进行一些调试.你应该使用真正的调试工具而不是拼凑一些东西.
aci*_*azz 25
只需将数据传输到error_log():
error_log(print_r($_REQUEST, true));
php内置服务器将输出写入php://stdout流,这意味着您可以向其中输出任何内容,但这仅用于调试。
这是一个简单的示例,说明如何获得写入服务器控制台的结果:
<?php declare(strict_types=1);
/**
 * This is for development purpose ONLY !
 */
final class ServerLogger {
    /**
     * send a log message to the STDOUT stream.
     *
     * @param array<int, mixed> $args
     *
     * @return void
     */
    public static function log(...$args): void {
        foreach ($args as $arg) {
            if (is_object($arg) || is_array($arg) || is_resource($arg)) {
                $output = print_r($arg, true);
            } else {
                $output = (string) $arg;
            }
            fwrite(STDOUT, $output . "\n");
         }
    }
}
// usage example : 
ServerLogger::log('Hello, world!');
// outputting an array : 
ServerLogger::log($_SERVER);