未捕获的 RuntimeException:输出缓冲区中的意外数据
Uncaught RuntimeException: Unexpected data in output buffer
我在 XAMPP 服务器上创建了一个 Mysql 数据库,现在我想通过邮递员将数据插入数据库。
我所做的一切都是真的,但发生了意想不到的错误。
似乎错误是因为 app.php of Slim.
这是我的index.php。
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../includes/dboperation.php';
$app = new \Slim\App;
$app->post('/createuser', function (Request $req, Response $res) {
if (IsEmpty(array('name', 'password'), $res)) {
$reqdata = $req->getParsedBody();
$name = $reqdata['name'];
$pass = $reqdata['password'];
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$db = new DbOperation();
$result = $db->createUser($name, $hashed_password);
if ($result == USER_CREATED) {
$Message = array();
$Message['error'] = false;
$Message['msg'] = 'User had been created successfully !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(201);
} else if ($result == USER_FAIELD) {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Problem occurred !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(422);
} else {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Username is invalide !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus();
}
}
});
function IsEmpty($required_param, $response)
{
$er = false;
$er_code = '';
$request_param = $_REQUEST;
foreach ($required_param as $param) {
if (!isset($request_param[$param]) || strlen($request_param[$param]) <= 0) {
$er = true;
$er_code .= $param . ', ';
$response->write(json_encode($er_code));
}
}
if ($er) {
$err = array();
$err['error'] = true;
$err['msg'] = 'Losted param : ' . $er_code;
$response->write(json_encode($err));
}
return $er;
}
$app->run();
?>
这里是dboperation.php
<?php
class DbOperations
{
private $con;
function __construct()
{
require_once dirname(__FILE__) . '/dbconnect.php';
$db = new DbConnect;
$this->con = $db->connect();
}
public function createUser($Name, $Password)
{
if (!$this->NameValidation($Name)) {
$stmt = $this->con->prepare("INSERT INTO users(name,password) VALUES(?,?)");
$stmt->bind_param("ss", $Name, $Password);
if ($stmt->execute()) {
return USER_CREATED;
} else {
return USER_FAILED;
}
} else {
return USER_EXISTED;
}
}
private function NameValidation($Name)
{
$stmt = $this->con->prepare("SELECT id FROM users WHERE name = ?");
$stmt->bind_param("s", $Name);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
?>
这是错误:
Warning: Use of undefined constant DB_HOST - assumed 'DB_HOST' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 2
Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 3
Warning: Use of undefined constant DB_PASS - assumed 'DB_PASS' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 4
Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 5
Warning: Use of undefined constant USER_CREATED - assumed 'USER_CREATED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 7
Warning: Use of undefined constant USER_EXISTED - assumed 'USER_EXISTED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 8
Warning: Use of undefined constant USER_FAILED - assumed 'USER_FAILED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 9
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php:625 Stack trace: #0 E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php(333): Slim\App->finalize(Object(Slim\Http\Response)) #1 E:\xamp\htdocs\test\public\index.php(69): Slim\App->run() #2 {main} thrown in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php on line 625
实际上,当我传递 require 参数以插入时,我没有得到任何显示,这意味着显示了空白页面,当我不传递它们时,错误发生了,但我期望我编码的响应。
感谢您仔细阅读!
两件事。
首先,您还没有定义常量,这意味着您的日志中会生成警告。检查您的 constants.php
并确保实际定义了常量。
其次,您可能有 display_errors
。永远不要那样做。在您的 ini 文件中或使用 ini_set('display_errors', false);
将其关闭。
致命错误是由于 Output 在您预期之前已经开始。通过关闭 display_errors
,您的应用应该 运行(尽管您可能仍需要修复这些常量!)
为了获得最佳的错误记录体验,请将 error_reporting
设置为 -1
,关闭 display_errors
,然后设置自定义 error_log
。然后在终端中输入 tail -f /path/to/error_log
。您的通知、警告和错误现在将实时滚动过去,而不会扭曲您网页的显示。
我在 XAMPP 服务器上创建了一个 Mysql 数据库,现在我想通过邮递员将数据插入数据库。 我所做的一切都是真的,但发生了意想不到的错误。 似乎错误是因为 app.php of Slim.
这是我的index.php。
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../includes/dboperation.php';
$app = new \Slim\App;
$app->post('/createuser', function (Request $req, Response $res) {
if (IsEmpty(array('name', 'password'), $res)) {
$reqdata = $req->getParsedBody();
$name = $reqdata['name'];
$pass = $reqdata['password'];
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$db = new DbOperation();
$result = $db->createUser($name, $hashed_password);
if ($result == USER_CREATED) {
$Message = array();
$Message['error'] = false;
$Message['msg'] = 'User had been created successfully !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(201);
} else if ($result == USER_FAIELD) {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Problem occurred !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(422);
} else {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Username is invalide !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus();
}
}
});
function IsEmpty($required_param, $response)
{
$er = false;
$er_code = '';
$request_param = $_REQUEST;
foreach ($required_param as $param) {
if (!isset($request_param[$param]) || strlen($request_param[$param]) <= 0) {
$er = true;
$er_code .= $param . ', ';
$response->write(json_encode($er_code));
}
}
if ($er) {
$err = array();
$err['error'] = true;
$err['msg'] = 'Losted param : ' . $er_code;
$response->write(json_encode($err));
}
return $er;
}
$app->run();
?>
这里是dboperation.php
<?php
class DbOperations
{
private $con;
function __construct()
{
require_once dirname(__FILE__) . '/dbconnect.php';
$db = new DbConnect;
$this->con = $db->connect();
}
public function createUser($Name, $Password)
{
if (!$this->NameValidation($Name)) {
$stmt = $this->con->prepare("INSERT INTO users(name,password) VALUES(?,?)");
$stmt->bind_param("ss", $Name, $Password);
if ($stmt->execute()) {
return USER_CREATED;
} else {
return USER_FAILED;
}
} else {
return USER_EXISTED;
}
}
private function NameValidation($Name)
{
$stmt = $this->con->prepare("SELECT id FROM users WHERE name = ?");
$stmt->bind_param("s", $Name);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
?>
这是错误:
Warning: Use of undefined constant DB_HOST - assumed 'DB_HOST' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 2
Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 3
Warning: Use of undefined constant DB_PASS - assumed 'DB_PASS' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 4
Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 5
Warning: Use of undefined constant USER_CREATED - assumed 'USER_CREATED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 7
Warning: Use of undefined constant USER_EXISTED - assumed 'USER_EXISTED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 8
Warning: Use of undefined constant USER_FAILED - assumed 'USER_FAILED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 9
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php:625 Stack trace: #0 E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php(333): Slim\App->finalize(Object(Slim\Http\Response)) #1 E:\xamp\htdocs\test\public\index.php(69): Slim\App->run() #2 {main} thrown in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php on line 625
实际上,当我传递 require 参数以插入时,我没有得到任何显示,这意味着显示了空白页面,当我不传递它们时,错误发生了,但我期望我编码的响应。
感谢您仔细阅读!
两件事。
首先,您还没有定义常量,这意味着您的日志中会生成警告。检查您的 constants.php
并确保实际定义了常量。
其次,您可能有 display_errors
。永远不要那样做。在您的 ini 文件中或使用 ini_set('display_errors', false);
将其关闭。
致命错误是由于 Output 在您预期之前已经开始。通过关闭 display_errors
,您的应用应该 运行(尽管您可能仍需要修复这些常量!)
为了获得最佳的错误记录体验,请将 error_reporting
设置为 -1
,关闭 display_errors
,然后设置自定义 error_log
。然后在终端中输入 tail -f /path/to/error_log
。您的通知、警告和错误现在将实时滚动过去,而不会扭曲您网页的显示。