简体   繁体   English

在PHP内置Web服务器中打印一些东西

[英]Print something in PHP built-in web server

In python built-in web server when use print in function, it prints result in terminal ... 在python内置的web服务器中使用print函数时,它会在终端打印结果...

for example: 例如:

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 

How can I print something like this in PHP built-in web server? 如何在PHP内置Web服务器中打印这样的内容?

for example I want print $_POST in terminal. 例如,我想在终端打印$ _POST。 I use php -S 127.0.0.1:3000 for run PHP built-in web server. 我使用php -S 127.0.0.1:3000来运行PHP内置的Web服务器。

The development web server built in to PHP 5.4+ does not work in the way you want. 内置于PHP 5.4+开发Web服务器无法以您希望的方式工作。 That is, it's not a PHP process, and you can't have it run code for you. 也就是说,它不是一个PHP进程,你不能让它为你运行代码。

It's designed to serve PHP applications and content from the specified directory. 它旨在为指定目录中的PHP应用程序和内容提供服务。 The output of the server process is the access log . 服务器进程的输出是访问日志 You can write to the log using the error_log function, with a value of 4 as the message_type . 可以使用error_log函数写入日志,值为4作为message_type So, in theory, you could do something like 所以,理论上,你可以做类似的事情

ob_start();
var_dump($_POST);
error_log(ob_get_clean(), 4);

It sounds like you're trying to perform some debugging. 听起来你正试图进行一些调试。 You should be using real debugging tools instead of cobbling something together. 你应该使用真正的调试工具而不是拼凑一些东西。

只需将数据传输到error_log():

error_log(print_r($_REQUEST, true));

php built-in server writes output to the php://stdout stream , which mean you can output anything to it, but this should only be used for debugging. php内置服务器将输出写入php://stdout流,这意味着您可以输出任何内容,但这只应用于调试。

here's a quick example of how can you achieve the result of writing to the server console : 这是一个快速示例,说明如何实现写入服务器控制台的结果:

<?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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM