PHP 在某些情况下将缓冲区字符串打印到网页中?



我不知道如何解释它,但我要试一试。

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

我的控制器中有该操作:

/**
* @Route("/testJson")
*/
public function testJsonAction() {
$app = new SymfonyBundleFrameworkBundleConsoleApplication($this->get("kernel"));
$app->setAutoExit(false);
$opt = array("command" =>
"doctrine:generate:entity",
"--entity" => "GuervylEditorBundle:TestOnline",
"--fields" => "kl:string");
$input = new SymfonyComponentConsoleInputArrayInput($opt);
$out = new SymfonyComponentConsoleOutputBufferedOutput();
$app->run($input, $out);
$out->fetch();
return new JsonResponse(json_encode(["a" => "b", "c" => "d"]));
}

从本地和托管服务器调用此操作会返回"{u0022au0022:u0022bu0022,u0022cu0022:u0022du0022}",并且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 SymfonyBundleFrameworkBundleConsoleApplication($mykernel);
$app->setAutoExit(false);
$opt = array("command" => $cmd) + $opt;
$input = new SymfonyComponentConsoleInputArrayInput($opt);
$out = new SymfonyComponentConsoleOutputBufferedOutput();
$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来修复它吗?因为我真的需要在该主机上托管我的网站,因为它为我提供了我需要的强大功能,但其他功能没有,或者它们太贵了。

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

最新更新