PHP 在某些情况下将缓冲区字符串打印到网页中? [英] PHP prints out a buffer string into web-page on certain circuntances?

查看:64
本文介绍了PHP 在某些情况下将缓冲区字符串打印到网页中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何解释,但我会尝试一下.

I don't know how to explain it but, I'm gonna give it a try.

这个问题涉及 2 个服务器,一个本地服务器和一个托管服务器.两台服务器都运行相同的 PHP 版本,即 7.0 [具有几乎相同的配置].和 2 个控制器动作.问题来自下面代码中的 $app->run($input, $out);.

This problem concerns 2 servers, a local and a hosting server. Both servers are running the same PHP version which is 7.0 [with almost same configurations]. And 2 controller actions. And the problem comes from $app->run($input, $out); from the codes below.

我的控制器中有该操作:

I have in my controller that action:

     /**
     * @Route("/testJson")
     */
    public function testJsonAction() {
        $app = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->get("kernel"));
        $app->setAutoExit(false);

        $opt = array("command" =>
            "doctrine:generate:entity",
            "--entity" => "GuervylEditorBundle:TestOnline",
            "--fields" => "kl:string");

        $input = new \Symfony\Component\Console\Input\ArrayInput($opt);

        $out = new \Symfony\Component\Console\Output\BufferedOutput();

        $app->run($input, $out);

        $out->fetch();

        return new JsonResponse(\json_encode(["a" => "b", "c" => "d"]));
    }

从本地和托管服务器调用此操作将返回 "{\u0022a\u0022:\u0022b\u0022,\u0022c\u0022:\u0022d\u0022}" 并带有 Content-类型
application/json
太好了,这是预期的结果.

Calling this action from the local and hosting server returns "{\u0022a\u0022:\u0022b\u0022,\u0022c\u0022:\u0022d\u0022}" and with Content-Type
application/json
which is great, it's the expected result.

问题来了:

与上面几乎相同的代码,我将它设置在另一个类中,我从另一个控制器操作中调用它,该操作通过来自不同类的 4 个方法来调用具有上述代码的方法 [callCommand]

That almost same code above, I set it inside another class, I call it from another controller action, which passes through 4 methods from different classes to call the method that has the code above [callCommand]

这是实现代码的方法:

public function callCommand($cmd, $opt, &$mykernel = null) {
        if ($mykernel == NULL) {
            $mykernel = new myKernel("dev", false, __DIR__ . "/../Resources/template_2.8/app");
        }

        $app = new \Symfony\Bundle\FrameworkBundle\Console\Application($mykernel);
        $app->setAutoExit(false);

        $opt = array("command" => $cmd) + $opt;

        $input = new \Symfony\Component\Console\Input\ArrayInput($opt);

        $out = new \Symfony\Component\Console\Output\BufferedOutput();

        $app->run($input, $out);
    }

从其他控制器操作中,我还在最后返回了一个 json 内容.我无法显示代码,因为它太大了.

From that other controller action, I also return a json content at the end. I can't show the code because it's too big.

当我从本地主机调用该控制器操作时,我得到了 JSON 内容和 Content-Type: application/json,这很好.

When I call that controller action from my localhost, I get the JSON content and Content-Type: application/json which is fine.

但是从托管服务器调用它我会得到额外的文本,例如:

But calling it from the hosting server I get extra texts like:

Entity generation  


  created ./src/Guervyl/EditorBundle/Entity/TestCase.php
> Generating entity class src/Guervyl/EditorBundle/Entity/TestCase.php: OK!
> Generating repository class src/Guervyl/EditorBundle/Repository/TestCaseRepository.php: OK!


  Everything is OK! Now get to work :).

调用$app->run($input, $out);时控制台的输出文本.之后,我得到了我设置的 HTTP 标头,然后是 json 内容.并且内容类型是 application/x-httpd-php5.

Which is the output texts from the console when calling $app->run($input, $out);. After that I get the HTTP header that I set then the json content. And also the content-type is application/x-httpd-php5.

该错误仅发生在特定的托管服务器上.我测试了其他托管服务器,代码在我的本地服务器上工作.

That error only happens on a specific hosting server. I tested other hosting server the code works like on my local server.

我的问题是为什么我会在该特定主机上收到错误消息?有什么我可以从 PHP.ini 更改来修复它的吗?因为我真的需要在该主机上托管我的网站,因为它为我提供了我需要的很棒的功能,但其他功能没有,或者它们太贵了.

My question is why am I getting the error on that specific hosting? Is there something I can change from the PHP.ini to fix it? Because I really need to host my website on that hosting because it offers me great features that I need but the others don't or they are too expensive.

推荐答案

好吧,在我调试代码后,我注意到错误发生了,因为我没有设置 --no-interaction 选项.因此,如果没有该选项,Symfony 会在没有为实体指定字段时等待输入.

Well, After I debugged the code I noticed that error happened because I did not set the --no-interaction option. So without that option, Symfony was waiting for input when no fields are specified for an Entity.

这篇关于PHP 在某些情况下将缓冲区字符串打印到网页中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆