【问题标题】:PHP prints out a buffer string into web-page on certain circuntances?PHP 在某些情况下将缓冲区字符串打印到网页中?
【发布时间】:2018-08-16 03:55:00
【问题描述】:

我不知道怎么解释,但是,我会试一试。

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

我的控制器中有这个动作:

     /**
     * @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-Type
application/json
,这很好,这是预期的结果。

现在问题来了:

上面几乎相同的代码,我将它设置在另一个类中,我从另一个控制器动作中调用它,它通过来自不同类的4个方法来调用具有上述代码的方法[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 内容。 我无法显示代码,因为它太大了。

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

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

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

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

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

【问题讨论】:

    标签: php symfony outputstream


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多