处理另一个错误时发生错误:yii\web\HeadersAlreadySentException: Headers already sent
Posted
技术标签:
【中文标题】处理另一个错误时发生错误:yii\\web\\HeadersAlreadySentException: Headers already sent【英文标题】:An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent处理另一个错误时发生错误:yii\web\HeadersAlreadySentException: Headers already sent 【发布时间】:2020-10-15 08:06:44 【问题描述】:我正在用 yii2 开发一个 rest api web 服务。 作为 localhost 上的 json 响应,json 响应我的 null 对象结束。 并在主机上出现 json 错误结束。
在 localhost 中,我收到的响应如下: 本地主机上的响应:
"status": 1,
"data":
"data": "Factor Api"
null
但在服务器上,我收到的响应如下: 服务器上的响应:
"status": 1,
"data":
"data": "Factor Api"
<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/factorap/public_html/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response->send()
#2 /home/factorap/public_html/vendor/yiisoft/yii2/base/ErrorHandler.php(135): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 main
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(656): yii\web\Response->send()
#2 /home/factorap/public_html/common/components/Api.php(56): yii\base\Application->end()
#3 /home/factorap/public_html/api/controllers/SiteController.php(92): common\components\Api->sendSuccessResponse(Array)
#4 [internal function]: api\controllers\SiteController->actionIndex()
#5 /home/factorap/public_html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#6 /home/factorap/public_html/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
#7 /home/factorap/public_html/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('', Array)
#8 /home/factorap/public_html/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('', Array)
#9 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 /home/factorap/public_html/api/v2/index.php(17): yii\base\Application->run()
#11 main</pre>
common/components/Api.php 第 53 行 = $this->setHeader(200);
我发送响应的功能: api函数:
public function sendSuccessResponse($data = false,$additional_info = false)
$this->setHeader(200);
$response = [];
$response['status'] = 1;
if (is_array($data))
$response['data'] = $data;
if ($additional_info)
$response = array_merge($response, $additional_info);
$response = Json::encode($response, JSON_PRETTY_PRINT);
//$strpos = strpos($response,'null');
if (isset($_GET['callback']))
/* this is required for angularjs1.0 client factory API calls to work */
$response = $_GET['callback'] . "(" . $response . ")";
echo $response;
else
echo $response;
Yii::$app->end();
和要运行的控制器动作。 控制器动作:
public function actionIndex()
Yii::$app->api->sendSuccessResponse(['data' => 'Factor Api']);
请帮我修复它们。
【问题讨论】:
不要回显输出,返回它。 【参考方案1】:还有另一种方法可以做到这一点。像这样:frontend/controllers/ApiController.php
class ApiController extends \yii\rest\Controller
public function behaviors()
return [
'contentNegotiator' => [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => yii\web\Response::FORMAT_JSON,
],
],
];
public function actionApi()
$model = \common\models\User::find()->one();
return $model->getAttributes();
frontend/config/main.php
:
return [
// other code
'components' => [
// other code
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'Ci@1ouPTLKXn9s6myzsfsef2UtTfKwjpnWO',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
['class' => 'yii\rest\UrlRule', 'controller' => 'api'],
],
],
// other code
],
];
打开http://yoursite.com/api/users
id: 1,
username: "admin",
auth_key: "IVi31eKxa-EE3ki7X7ToKb6uO8dknNrV",
password_hash: "$2y$13$oMiNRYUYb0gPcWpheiYdb.0Xs/lWk7Ixcb.F5ch38ExbiSok1TfRi",
password_reset_token: null,
email: "admin@admin.lc",
status: 10,
created_at: 1603274263,
updated_at: 1603274263,
verification_token: "cTnvRiub3BpoCEwx-yFUb10zCtu0TpxK_1603274263",
【讨论】:
以上是关于处理另一个错误时发生错误:yii\web\HeadersAlreadySentException: Headers already sent的主要内容,如果未能解决你的问题,请参考以下文章