php - 未捕获的 RuntimeException:输出缓冲区中的意外数据
问题描述
我在 XAMPP 服务器中创建了一个 Mysql 数据库,现在我想通过邮递员将数据插入数据库。我做了所有真实但意外的错误。似乎错误是因为 Slim 的 app.php。
这是我的 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
实际上,当我传递要求参数以插入时,我什么也没有显示,这意味着已显示空页面,当我不传递它们时,会发生错误,但我期望我编码的响应。
感谢您仔细阅读本文!
解决方案
两件事情。
首先,您还没有定义常量,这意味着您的日志中会生成警告。检查constants.php
并确保实际定义了常量。
其次,你可能有display_errors
。永远不要那样做。在您的 ini 文件中或使用ini_set('display_errors', false);
.
致命错误是由输出在您预期之前已经开始引起的。通过display_errors
关闭,您的应用程序应该运行(尽管您可能仍需要修复这些常量!)
为获得最佳错误记录体验,请设置error_reporting
为-1
、display_errors
关闭并设置自定义error_log
. 然后在终端中输入tail -f /path/to/error_log
。您的通知、警告和错误现在将实时滚动过去,而不会扭曲您网页的显示。
推荐阅读
- web-scraping - 在谷歌表格中抓取历史股票价格
- javascript - JavaScript:在不点击显示按钮的情况下显示街景地图
- r - R中的线性优化,斯蒂格勒饮食问题
- apache-spark - 从 pyspark 中的列表列表中生成多个列表
- jupyter-notebook - 防止 jupyterlab 为所有打开的选项卡自动启动新会话
- android - 如何将我的 android 提示计算器更改为 MVC 设计?
- python - VSCode 中的失控绝地语言任务
- python - Pyspark,获取计数增加到列表中最高计数的第一个日期
- deep-learning - 蛇种图像分类表现不佳
- string - Fortran 如何在未格式化的二进制文件中找到字符串?